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

Teach you how to distinguish between single-mode and multi-mode optical fibers

Single mode fiber Single-mode fiber has a thinner...

Challenges of Deploying Wireless Mesh Networks

Wireless mesh networks have been around since the...

Design and implement a TCP protocol semi-connected port scanner

A student fan sent a question: From the title, yo...

UK 3G networks to end in 2025

British telecom operator Virgin Media O2 (VMO2) h...

How does Sentinel intercept abnormal traffic?

[[342079]] When you use electricity at home, you ...

An article explains the principles of Docker network

Overview Docker native network is based on Linux ...

RackNerd "Memorial Day" Sale: Los Angeles VPS from $14.99 per year

RackNerd has launched a Memorial Day promotion, w...

Insufficient CMDB Momentum = “Failed” IT Operations?

Whether it is the implementation of ITIL in the e...

Tested in 6 cities! How fast can 5G uplink with Super Uplink run?

Ever since 5G has entered the homes of ordinary p...

DMIT: $6.9/month-750MB/10G SSD/2TB/Hong Kong & Japan data centers

It has been a while since I shared information ab...