This article mainly introduces the definition of global data, functions reuse, templates, etc. Instance introduction related information of WeChat applet. Friends who need it can refer to it
WeChat applet defines global data, function reuse, templates and other issues summary:
1. How to define global data
In app.js The data or functions defined in 's App({}) are global and can be called in the page by var app = getApp(); app.function/key, but we don’t need to define global functions in app.js.
2. How to achieve code reuse
Function reuse:
test.js test: function(){ } module.exports={ test:test } other.js var common = require('test.js'); page({ common.test() })
Template:
<template name="odd"> <view> odd </view> </template> <template name="even"> <view> even </view> </template> <block wx:for="{{[1, 2, 3, 4, 5]}}"> <template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/> </block> //我们页可以把模板定义在其他文件中,以<import src="url"/>的形式引入,但是import有作用域的概念,即只会import目标文件中定义的template, 而不会import目标文件import的template //include可以将目标文件除了<template/>的整个代码引入,相当于是拷贝到include位置。
3. For attributes whose value is boolean type in the component, such as progress The active attribute of the component, the checked attribute of the checkbox, etc. This attribute takes effect regardless of whether it is set to true or false. The test found that this is also the case in HTML, but it can be rendered successfully through checked={{}}.
Thank you for reading, I hope it can help you, thank you for your support of this site!
The above is the detailed content of Introduction to examples of defining global data, function reuse, templates, etc. for WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!