微信小程序:漫画小程序项目总结
项目考察点:1、wx:for 循环的使用。? 项目要求:1、构建 WXML 模板。? wx:for 循环的使用index.js:
index.wxml: <import src="template.wxml"/><viewclass"container"><templateis"imgItem"data"{{images}}"</view> 由于循环结构通过 template 形成独立模板文件,因此 wxml 文件只需引入模板。{{images}} 为 js 文件data中的 images。imgItem 对应模板文件中的 name 属性。 模板template: <blockwx:forwx:for-item"item"wx:for-index"index"wx:key"unique"></block> wx:for-item 指定当前项变量名,wx:for-index 指定数组下标变量。如果不指定 wx:for-item 和 wx:for-index,数组当前项的变量名默认为 item,默认数组的当前项的下标变量名默认为 index。(文档戳这:小程序列表渲染) 因此项目中 images[index] 可以用 item 代替。 template.wmxl: "unique"bind:tap"toggleText"data-value"{{item.aside}}"data-unique"{{item.unique}}"> <text"{{item.aside?'textShow':'textHide'}}""text"{{tools.commaReplace(item.text)}}</text> </template> 项目要求通过 wxs 处理半角变全角逗号问题,引入 wxs 用法是,其中 tools 是 wxs 的名称,通过 module 定义。在{{tools.commaReplace(item.text)}}中,.commaReplace 由 wxs 定义,() 中传入 js 数据。 半角变全角逗号wxs: 在小程序中, 由于运行环境的差异,在 iOS 设备上小程序内的 wxs 会比 javascript 代码快 2 ~ 20 倍。 index.wxs: function commaReplace(some_msg){ some_msg = some_msgreplace","); return; moduleexports { }; 使用 while 循环遍历字符串中每个字符的是否与半角逗号,匹配,如匹配使用 replace(",",",") 方法替换,module.exports 输出模板数据供 wxml 调用。 旁白的显示与隐藏
var tag etargetdatasettag; value (!value); +'].aside';//设置images数组动态变量aside iftag ==='image'){ value true; }) else'text'){ value } 在小程序里,想要更新页面的数据,必须使用 this.setData 对数据进行更新。
数组数据下标为动态数据时的取值方法: 项目中我采用的方式是给 aside 赋值,然后在 this.setData 中输出 [aside] 的方式。 还有另一种更高级的写法,使用 JSON. stringify 把要设置的数据进行序列化,或者使用字符串拼接的方法拼出 json ,然后再重新JSON.parse 传给 setData:+'].aside":'thisdata ]toString()'}';JSONparsejson)); |