Home > WeChat Applet > Mini Program Development > Introduction to examples of defining global data, function reuse, templates, etc. for WeChat mini programs

Introduction to examples of defining global data, function reuse, templates, etc. for WeChat mini programs

高洛峰
Release: 2017-03-15 16:15:32
Original
2960 people have browsed it

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()
})
Copy after login

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 ? &#39;even&#39; : &#39;odd&#39;}}"/>
</block>
//我们页可以把模板定义在其他文件中,以<import src="url"/>的形式引入,但是import有作用域的概念,即只会import目标文件中定义的template,
而不会import目标文件import的template
//include可以将目标文件除了<template/>的整个代码引入,相当于是拷贝到include位置。
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template