Skip to content

WXS模塊

WXS代碼可以編寫在wxml文件中的<wxs>標籤內,或以.wxs為后綴名的文件內。

WXS模塊

每一個.wxs文件和<wxs>標籤都是一個單獨的模塊。

每個模塊都有自己獨立的作用域。 即在一個模塊裡面定義的變量與函數,默認為私有的,對其他模塊不可見。

一個模塊要想對外暴露其內部的私有變量與函數,只能通過module.exports實現。

.wxs 文件

在開發者工具裡面,右鍵可以直接創建.wxs文件,在其中直接編寫WXS腳本。

示例代碼:

js
// /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>標籤引用。

module 對象

每個wxs模塊均有一個內寘的module對象。

内容

exports:通過該内容,可以對外共亯本模塊的私有變量與函數。

示例代碼:

js
// /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";
js
<!-- page/index/index.wxml -->

<wxs src="./../tools.wxs" module="tools" />
<view> {{tools.msg}} </view>
<view> {{tools.bar(tools.FOO)}} </view>

頁面輸出:

js
some msg
'hello world' from tools.wxs

require函數

在.wxs模塊中引用其他wxs文件模塊,可以使用require函數。

引用的時候,要注意如下幾點:

  • 只能引用.wxs文件模塊,且必須使用相對路徑;

  • wxs模塊均為單例,wxs模塊在第一次被引用時,會自動初始化為單例對象,多個頁面,多個地方,多次引用,使用的都是同一個wxs模塊對象;

如果一個wxs模塊在定義之後,一直沒有被引用,則該模塊不會被解析與運行。

示例代碼:

js
// /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";
js
// /pages/logic.wxs

var tools = require("./tools.wxs");

console.log(tools.FOO);
console.log(tools.bar("logic.wxs"));
console.log(tools.msg);
js
<!-- /page/index/index.wxml -->

<wxs src="./../logic.wxs" module="logic" />

控制台输出:

js
'hello world' from tools.wxs
logic.wxs
some msg

<wxs>標籤

内容名類型預設值說明
modulestring-當前<wxs>標籤的模塊名,必填欄位
srcstring-引用.wxs文件的相對路徑,僅當本標籤為單閉合標籤或標籤的內容為空時有效

module属性

module属性是當前<wxs>標籤的模塊名。 在單個wxml文件內,建議其值唯一,有重複模塊名則按照先後順序覆蓋(後者覆蓋前者),不同文件之間的wxs模塊名不會相互覆蓋。

module屬性值的命名必須符合下麵兩個規則:

  • 首字元必須是:字母(a-zA-Z),底線(_);
  • 剩餘字元可以是:字母(a-zA-Z),底線(_),數位(0-9)。

示例代碼:

js
<!--wxml-->

<wxs module="foo">
var some_msg = "hello world";
module.exports = {
  msg : some_msg,
}
</wxs>
<view> {{foo.msg}} </view>

頁面輸出:

js
hello world

上面例子聲明了一個名字為foo的模塊,將some_msg變量暴露出來,供當前頁面使用。

src内容

src内容可以用來引用其他的wxs文件模塊。

引用的時候,要注意如下幾點:

  • 只能引用.wxs文件模塊,且必須使用相對路徑;

  • wxs模塊均為單例,wxs模塊在第一次被引用時,會自動初始化為單例對象,多個頁面,多個地方,多次引用,使用的都是同一個wxs模塊對象;

  • 如果一個wxs模塊在定義之後,一直沒有被引用,則該模塊不會被解析與運行。

示例代碼:

js
// /pages/index/index.js

Page({
  data: {
    msg: "'hello wrold' from js",
  }
})
js
<!-- /pages/index/index.wxml -->

<wxs src="./../comm.wxs" module="some_comms"></wxs>
<!-- You can also directly use single tag closed syntax.
<wxs src="./../comm.wxs" module="some_comms" />
-->

<!-- Calls the bar function in the some_comms module, and the parameter is foo in the some_comms module. -->
<view> {{some_comms.bar(some_comms.foo)}} </view>
<!-- Calls the bar function in the some_comms module, and the parameter is msg in page/index/index.js. -->
<view> {{some_comms.bar(msg)}} </view>

頁面輸出:

js
'hello world' from comm.wxs
'hello wrold' from js

上述例子在文件/page/index/index.wxml中通過<wxs>標籤引用了/page/comm.wxs模塊。

注意事項

  • <wxs>模塊只能在定義模塊的WXML文件中被訪問到。 使用<include>或<import>時,< wxs>模塊不會被引入到對應的WXML文件中;

  • <template>標籤中,只能使用定義該<template>的WXML文件中定義的<wxs>模塊。