Home > WeChat Applet > Mini Program Development > Introduction to defining global data and function reuse and templates in WeChat mini programs

Introduction to defining global data and function reuse and templates in WeChat mini programs

不言
Release: 2018-06-26 16:58:49
Original
2938 people have browsed it

This article mainly introduces relevant information on the definition of global data, function reuse, templates, etc. in WeChat mini programs. Friends who need it can refer to

WeChat mini programs define global data and function reuse. , templates and other issues summary:

1. How to define global data

The data or functions defined in App({}) of app.js are all global , in the page, you can call through var app = getApp(); app.function/key, but we do not need to define the global function 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 the active attribute of the progress component, checkbox The checked attribute and so on. 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={{}}.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the analysis of the selector (time, date, region) of the WeChat applet

WeChat Implementation of the like, delete list and sharing functions of the mini program

The above is the detailed content of Introduction to defining global data and function reuse and templates in 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