Front-end: Uniapp encapsulation network request notes

Front-end: Uniapp encapsulation network request notes

[[425641]]

As a front-end framework for mobile development, uniapp is very popular in China. Using HbuilderX development tools, systems developed based on the uniapp framework can be easily converted into small programs, apps and other mobile programs, greatly reducing the cost of mobile development. Network requests are a necessary technology for every front-end project, so it is necessary to encapsulate front-end network requests. Today, I will introduce to you how to implement simple encapsulation of network requests based on uniapp. I hope it will be helpful to novices!

1. First install the HbuilderX development tool to create a uniapp project.

2. Create config, js, and request.js files in the common directory

  1. const BASE_URL = "https://qqlykm.cn/api/yan/gc.php" ; // Randomly query ancient poetry interface

request.js

  1. import {GlobeConfig} from   'config.js'  
  2. export const request = (options)=>{
  3. return new Promise((resolve, reject)=>{
  4. // Encapsulation body: network request
  5. uni.request({
  6. url: "/api" +options.url,
  7. data: options.data || {},
  8. // The default value is GET. If you need to change it, set other method values ​​in options.
  9. method: options.method || 'GET' ,
  10. success: (res) => {
  11. console.log(res.data); // The console displays data information
  12. resolve(res)
  13. },
  14. fail: (err) =>{
  15. // The pop-up box on the page failed to display
  16. uni.showToast({
  17. Title: 'Request interface failed'  
  18. })
  19. // Return error message
  20. reject(err)
  21. },
  22. catch:(e)=>{
  23. console.log(e);
  24. }
  25. })
  26. }
  27. )
  28. }
  29. // https://qqlykm.cn/api/yan/gc.php test interface
  30. { "code" : "200" , "msg" : "success" ,
  31. "data" :
  32. { "Poetry" : "The yes of a thousand people is not as good as the outspokenness of one scholar." ,
  33. "Poet" : "null" ,
  34. "Poem_title" : "Records of the Grand Historian: Biography of Shang Yang" }
  35. }

3. main.js imports the encapsulated network request

  1. //Import the encapsulated network request
  2. import {request} from   'common/request.js'  
  3. Vue.prototype.$request = request

4. Create a new test demo.vue

  1. <template>
  2. < view class= "content" >
  3. < view class= "article-meta" >
  4. <text class= "" >{{Poem_title}}</text>
  5. </ view >
  6. < view >
  7. <text class= "" >Author: {{Poet}}</text>
  8. </ view >
  9. < view class= "article-content" >
  10. < view >{{Poetry}}</ view >
  11. </ view >
  12. </ view >
  13.  
  14. </template>
  15.  
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. Poem_title: "" ,
  21. Poet: "" ,
  22. Poetry: ""  
  23. }
  24. },
  25. onLoad() {
  26. this.initData();
  27. },
  28.  
  29. methods: {
  30. async initData() {
  31. debugger;
  32. const res = await this.$request({
  33. url: '' , //The requested url can be written in the configuration file by default
  34. data: {
  35. //Interface request parameters, may be empty
  36. }
  37. })
  38. // Assign data to the page
  39. if (res.data.msg == "success" ) {
  40. this.Poem_title = res.data.data.Poem_title;
  41. this.Poet = res.data.data.Poet== "null" ? "Anonymous" : res.data.data.Poet;
  42. this.Poetry = res.data.data.Poetry;
  43. }
  44.  
  45. }
  46. }
  47. }
  48. </script>
  49.  
  50. <style lang= "scss" scoped>
  51. </style>

Run Page

Personal blog website: https://programmerblog.xyz

This article is reprinted from the WeChat public account "IT Technology Sharing Community", which can be followed through the following QR code. To reprint this article, please contact the IT Technology Sharing Community public account.

<<:  Three-minute review! A quick overview of 5G industry development trends in September 2021

>>:  How to force close TCP connection in Go

Recommend

What are the differences and connections between 25G/50G/100G technologies?

In the past decade, 10G and 40G technologies have...

HTTP 3: Past, Present, and Early Adoption

HTTP/3 has reached another milestone: Recently, C...

GSA report: 63 operators around the world have launched commercial 5G services

The latest global 5G network development report f...

SDN reshapes enterprise networks and changes the role of network managers

We have seen that many operators have significant...

Gartner: China's IT spending is expected to exceed US$550 million in 2022

According to Gartner's latest forecast, globa...

Demystifying gRPC: Unleashing Lightning-Speed ​​Communication

Before we dive into the details of gRPC, it is im...

A network administrator's self-cultivation: TCP protocol

Today, let’s continue with the network administra...