网络编程 发布日期:2025/11/12 浏览次数:1
微信小程序 定义全局数据、函数复用、模版等问题总结:
1.如何定义全局数据
在app.js的App({})中定义的数据或函数都是全局的,在页面中可以通过var app = getApp(); app.function/key的方式调用,不过我们没有必要再app.js中定义全局函数。
2.如何实现代码的复用
函数的复用:
test.js
test: function(){
}
module.exports={
test:test
}
other.js
var common = require('test.js');
page({
common.test()
})
模板:
<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 "/>
</block>
//我们页可以把模板定义在其他文件中,以<import src="/UploadFiles/2021-04-02/url">
3.对于组件中值为boolean类型的属性,比如progress组件的active属性,checkbox的checked属性等等。无论设置成true还是false该属性都生效,测试发现html中也有这种情况,但通过checked={{}}的方式可以渲染成功。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!