This article mainly introduces the mini-program development skills and the following traps:
1. page [pages/XXX] not found. May be caused by: 1. Forgot to add page route in app. json.2. Invoking Page () in async task appears.
As follows:
Solution: add page () to every XXX. js file to generate a page instance object.
Page({});
Sometimes, even if you write the basic page () code in each js file, this error is still guaranteed. pay attention to the app. the routing order in json. for example, the second-level interface is generally written under the first-level interface. do not write it at the end. for example:
Correct syntax:
At the same time, you must note that the default index and logs are usually placed at the end, sometimes I am placed in the middle part, and the lower part of the page will also appear. sometimes this error is just a problem with development tools. restart it (there are too many bugs in the first version of the development tool in public beta ).
2. network requests: for network requests is also a big pitfall. as always, small programs can only use https, unless the development tool after the use of crack, and cannot run in the real machine. https://github.com/gavinkwoe/weapp-ide-crack
For network requests, the most common use is POST requests, but this is the big pitfall of small programs. the official documents are simple and not enough to get started. for post requests, we need to change the data to formData and pass it over.
First, write the following code in the Util. js file in the Utils folder to convert the parameter to formData.
// Convert the parameter data to formDatafunction json2Form (json) {var str = []; for (var p in json) {str. push (encodeURIComponent (p) + "=" + encodeURIComponent (json [p]);} return str. join ("&");} module. exports. json2Form = json2Form
Then introduce the Util. js file in the post request file.
var Util = require( '../../../utils/util');
Next, modify the request method:
// Call the method of the application instance to obtain global data wx. request ({url: 'http: // www.pintasty.cn/productdetail/findproductdetailbyid', method: 'post', header: {"content-type ": "application/x-www-form-urlencoded" // here, Content-Type can be used at the beginning. Currently, only content-type}, data: Util. json2Form ({product_id: "P-2c22ad5be9b24a5da0cd5d8594e8ec32", uploadImgId: "UU-60a0e559b0a0421d892232fc75ee9d17"}), // you have to modify it here, the parameter can only be passed in success: function (res) {console. log ('wine details' + JSON. stringify (res) that. setData ({wineData: res. data. data, bigPicture: res. data. data. bigPicture})}, fail: function (res) {console. log ('failed' + JSON. stringify (res ))},})
Then let's do it. because my development tool is a cracked version, you can use http.
3. obtain the screen width and height in the XXX. js file.
Based on the getSystemInfo () method provided by the applet, this method can obtain common device information, such as the mobile phone model. device pixel ratio. screen width and height. the most commonly used is the screen width and height.
// Obtain the screen width var screenWidth = wx. getSystemInfo ({success: function (res) {screenWidth = res. required wwidth}) // Obtain the screen height var screenHeight = wx. getSystemInfo ({success: function (res) {screenHeight = res. windowHeight} Page ({onLoad: function () {console. log ('screen height: '+ screenHeight) console. log ('screen width: '+ screenWidth )}})
Suggestion: in the design draft, if the device width is px, it is easier to calculate px, 1rpx = 1px. in this way, the size of the size measured on the design drawing is as much as rpx, as for how many rem files need to be converted to on different devices, it is handed over to the applet for self-conversion.
4. sublime: mini-program components and API completion plug-ins
Http://www.cnblogs.com/zhengrunlin/p/5951224.html? Hmsr = toutiao. io & utm_medium = toutiao. io & utm_source = toutiao. io
5. an Official Style Library has been launched to facilitate development. for some similar UI interfaces, we need to introduce them without repeating the wheel.
Add github address: https://github.com/weui/weui-wxss
We only need to import weui. wxss and so on to reduce a lot of css layout work.
For more tips on small program development and related articles on trap filling, please follow the PHP Chinese website!