May 18, 2021 WeChat Mini Program Development Document
WXS code can be written in the wxml file within the label, or in a file with the .wxs suffix.
Each .wxs file and the label are a separate module.
Each module has its own unique scope. That is, variables and functions defined in one module are private by default and are not visible to other modules.
A module can only be implemented by module.exports if it wants to expose its internal private variables and functions.
In the WeChat developer tool, right-click to create a .wxs file directly, where you can write WXS scripts directly.
Example code:
// /pages/comm.wxs
var foo = "'hello world' from comm.wxs";
var bar = function(d) {
return d;
}
module.exports = {
foo: foo,
bar: bar
};
The above examples are written in the /pages/comm.wxs file. The .wxs file can be referenced by other .wxs files or by tags in WXML.
Each wxs module has a built-in module object.
Example code:
// /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";
{{tools.msg}}
{{tools.bar(tools.FOO)}}
Page output:
some msg
'hello world' from tools.wxs
References to other wxs file modules in the .wxs module, you can use the require function.
When referring, pay attention to the following points:
Sample code:
// /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);
Console output:
'hello world' from tools.wxs
logic.wxs
some msg
The property name | Type | The default | Description |
---|---|---|---|
module | String |
The
name of the module for the current
label. R
equired fields.
|
|
Src | String | Refers to the relative path of the .wxs file. Valid only if the label is a single closed label or if the contents of the label are empty. |
The module property is the module name of the current label. W ithin a single wxml file, it is recommended that its value be unique. R epeated module names are overwritten in order (the latter overrides the former). The wxs module names between the different files are not overwritten on top of each other.
The module property value must be named in accordance with two rules:
Example code:
<!--wxml-->
<wxs module="foo">
var some_msg = "hello world";
module.exports = {
msg : some_msg,
}
</wxs>
<view> {{foo.msg}} </view>
Page output:
hello world
The example above declares a module named foo that exposes some_msg variable for use by the current page.
The src property can be used to reference other wxs file modules.
When referring, pay attention to the following points:
Example code:
// /pages/index/index.js
Page({
data: {
msg: "'hello world' from js",
}
})
<!-- 也可以直接使用单标签闭合的写法
-->
{{some_comms.bar(some_comms.foo)}}
{{some_comms.bar(msg)}}
Page output:
'hello world' from comm.wxs
'hello wrold' from js
The above example refers to the /page/comm.wxs module in file /page/index/index.wxml via the tab.