上一节,讲了模板的概念,其实小程序还提供了模块的概念。源码:https://github.com/limingios/wxProgram.git 中的No.8

小程序的WXS模块

js代码块可以在页面中被引入使用

定义*.wxs,module.exports暴露接口和属性从私有到公用的概念,通过暴露就可以公有话。


官方的阐述>https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxs/01wxs-module.html



演示模块的概念>每一个 .wxs 文件和 标签都是一个单独的模块。每个模块都有自己独立的作用域。即在一个模块里面定义的变量与函数,默认为私有的,对其他模块不可见。一个模块要想对外暴露其内部的私有变量与函数,只能通过 module.exports 实现。

<!wxs.wxml--><view class="container">  <wxs src="../wxs/module.wxs" module="item"></wxs>  <view>{{item.name}}</view>  <view>{{item.age}}</view>  <view>{{item.method("这是一个参数传递")}}</view>  <view>{{item.name}}</view>  <view>{{item.age}}</view>  <view>{{item.method("这是一个参数传递")}}</view>  <view>{{item.name}}</view>  <view>{{item.age}}</view>  <view>{{item.method("这是一个参数传递")}}</view></view>// module.wxsvar name ="公众号:编程坑太多"var age = 18;var method = function(obj){  return obj;}module.exports ={  name :name,  age : age,  method :method}



PS : 通过src进行导入,然后在引入module参数进行调用里面的属性和接口方法。


>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!>>原文链接地址:「小程序JAVA实战」小程序模块页面引用(18)