WXS 代码可以编写在 wxml 文件中的 <wxs> 标签内,,,或以 .wxs 为后缀名的文件内。。。。
每一个 .wxs 文件和 <wxs> 标签都是一个单独的模浚??。。。。
每个模浚??槎加凶约鹤粤Φ淖饔糜。。。。即在一个模浚??槟诶锝缢档谋淞坑牒,,默以为私有的,,,对其他模浚??椴豢杉。。。。
一个模浚??橐攵酝馓宦镀淠诓康乃接斜淞坑牒,,只能通过 module.exports 实现。。。。
在微信开发者工具内里,,,右键可以直接建设 .wxs 文件,,,在其中直接编写 WXS 剧本。。。。
示例代码:
// /pages/comm.wxs
var foo = "'hello world' from comm.wxs";
var bar = function(d) {
return d;
}
module.exports = {
foo: foo,
bar: bar
};
上述例子在 /pages/comm.wxs 的文件内里编写了 WXS 代码。。。。该 .wxs 文件可以被其他的 .wxs 文件 或 WXML 中的 <wxs> 标签引用。。。。
每个 wxs 模浚??榫幸桓瞿谥玫 module 工具。。。。
示例代码:
// /pages/tools.wxs
var foo = "'hello world' from tools.wxs";
var bar = function (d) {
return d;
}
module.exports = {
FOO: foo,
bar: bar,
};
module.exports.msg = "some msg";
<!-- page/index/index.wxml -->
<wxs src="./../tools.wxs" module="tools" />
<view> {{tools.msg}} </view>
<view> {{tools.bar(tools.FOO)}} </view>
页面输出:
some msg
'hello world' from tools.wxs
在.wxs模浚??橹幸闷渌 wxs 文件模浚??椋,,可以使用 require 函数。。。。
引用的时间,,,要注重如下几点:
示例代码:
// /pages/tools.wxs
var foo = "'hello world' from tools.wxs";
var bar = function (d) {
return d;
}
module.exports = {
FOO: foo,
bar: bar,
};
module.exports.msg = "some msg";
// /pages/logic.wxs
var tools = require("./tools.wxs");
console.log(tools.FOO);
console.log(tools.bar("logic.wxs"));
console.log(tools.msg);
<!-- /page/index/index.wxml -->
<wxs src="./../logic.wxs" module="logic" />
控制台输出:
'hello world' from tools.wxs
logic.wxs
some msg
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| module | String |
目今 <wxs> 标签的模浚??槊。。。。必填字段。。。。 |
|
| src | String | 引用 .wxs 文件的相对路径。。。。仅当本标签为单闭合标签或标签的内容为空时有用。。。。 |
module 属性是目今 <wxs> 标签的模浚??槊。。。。在单个 wxml 文件内,,,建议其值唯一。。。。有重复模浚??槊蚱局は群笏承蛄郑ê笳吡智罢撸。。。。差别文件之间的 wxs 模浚??槊换嵯嗷チ。。。。
module 属性值的命名必需切合下面两个规则:
示例代码:
<!--wxml-->
<wxs module="foo">
var some_msg = "hello world";
module.exports = {
msg : some_msg,
}
</wxs>
<view> {{foo.msg}} </view>
页面输出:
hello world
上面例子声明晰一个名字为 foo 的模浚??椋,,将 some_msg 变量袒露出来,,,供目今页面使用。。。。
src 属性可以用来引用其他的 wxs 文件模浚??。。。。
引用的时间,,,要注重如下几点:
示例代码:
// /pages/index/index.js
Page({
data: {
msg: "'hello wrold' from js",
}
})
<!-- /pages/index/index.wxml -->
<wxs src="./../comm.wxs" module="some_comms"></wxs>
<!-- 也可以直接使用单标签闭合的写法
<wxs src="./../comm.wxs" module="some_comms" />
-->
<!-- 挪用 some_comms 模浚??槟诶锏 bar 函数,,,且参数为 some_comms 模浚??槟诶锏 foo -->
<view> {{some_comms.bar(some_comms.foo)}} </view>
<!-- 挪用 some_comms 模浚??槟诶锏 bar 函数,,,且参数为 page/index/index.js 内里的 msg -->
<view> {{some_comms.bar(msg)}} </view>
页面输出:
'hello world' from comm.wxs
'hello wrold' from js
上述例子在文件 /page/index/index.wxml 中通过 <wxs> 标签引用了 /page/comm.wxs 模浚??。。。。
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。。。KESION 一直通过手艺立异,,,提供产品和服务,,,助力企业向数字化转型,,,通过科技驱动商业刷新,,,让商业变得更智慧!
引用 WXML提供两种文件引用方式 import 和 include 。。。。 import import 可以在该文件中使用目的文件界说的 template ,,,如: 在item.wxml中界说了一个叫 i
看法 WXS 中的变量均为值的引用。。。。 没有声明的变量直接赋值使用,,,会被界说为全局变量。。。。 若是只声明变量而不赋值,,,则默认值为undefined。。。。 var体现与javascript一致,,,会有变量提升。。。。 v