Introduction to Data Access to WeChat applets

Source: Internet
Author: User
This article mainly introduces detailed information about small program data access instances. For more information about small program data access instances, see this article, for more information, see

Let's talk about the structure of the applet.

1. each View (. wxml) only the script (. js) and style (. wxss). No reference is required. the scripts and styles under the page are inherited to the outermost app. js, app. wxcss

2. the script is also a. js file. it has a fixed format: page, which is used to obtain data.

3. utils is used to place data interfaces.

Data access is not a problem if you understand ajax.

Small programs, because the IDE is too bad, if the code is hard to read, the entire project will be difficult to maintain.

Because I have never written an app, I don't know how data access is encapsulated in the app.

As a small programmer with three days of work experience, I feel that if the data on each page is accessed by myself, it would be too hard to develop OOP.

Then I thought of linq to SQL, and only took two of them. I originally planned to use singelordefault and firstordefault. I thought it would be too troublesome. I used getbyparams, getbyid, search for all data based on the condition, or obtain a data entry based on the id.

Let's look at the method. it's a bit cool.


const API_URL = 'http://localhost:4424/api/'function getApi(url,params){ return new Promise((res,rej)=>{  wx.request({   url:API_URL+'/'+url,   data:Object.assign({},params),   header:{'Content-Type': 'application/json'},   success:res,   fail:rej  }) })}module.exports = { GetByParams(url,page=1,pageSize=20,search = ''){  const params = { start: (page - 1) * pageSize, pageSize: pageSize }  return getApi(url, search ? Object.assign(params, { q: search }) : params)   .then(res => res.data) }, GetById(url,id){  return getApi(url, id)   .then(res => res.data) }}

Module. exports = {} is a fixed writing method. each method is separated by commas.

I set a url parameter, because it is impossible to put all interfaces in a conntroller, so the url format is "conntroller/action"

Let's take a look at the called chestnuts to see how to use them.


Const req = require ('.. /.. /utils/util. js ') Page ({data: {imgUrls: [], indicatorDots: true, autoplay: true, interval: 2000, duration: 2000}, onLoad () {req. getByParams ('Home/homebanner ') // You can check it here. then (d => this. setData ({imgUrls: d, loading: false })). catch (e => {this. setData ({imgUrls: [], loading: false })})}})

This is the method used to obtain the banner graph of the index. req. GetByParams ('Home/homebanner ') can also be set with parameters or empty.

The final page is like this

In the red box on the right, we can see the data returned by the request, or modify the data on the right. the interface will change accordingly. this is about Debugging. Let's discuss it later.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is the detailed introduction of data access to small programs. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.