欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在wxml中直接寫js代碼(wxs)

 更新時間:2019年11月14日 09:54:54   作者:小魚吃貓  
這篇文章主要介紹了如何在wxml中直接寫js代碼(wxs),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了如何在wxml中直接寫js代碼(wxs),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

我們在h5開發(fā)中,很多時候要在html中寫到j(luò)s代碼,這個很容易實(shí)現(xiàn)。但是在微信小程序開發(fā)中,是不能直接在wxml中寫js代碼的,因此就有了wxs。在wxml中用wxs代碼,有以下幾種方式(在小程序文檔中寫的很清楚,我只不過是總結(jié)下)

第一種:直接在wxml文件中使用<wxs>標(biāo)簽

<wxs module="dateModule">
 var now = getDate();
 module.exports = {
  date: now
 }
</wxs>
<view>當(dāng)前時間:{{dateModule.date}}</view>

第二種:類似于js,寫一外部wxs文件,然后wxml中引用。對于這個,我直接引用官方文檔中的例子

// pages/dateTool.wxs
var now = getDate();
var format = function(lastDate) {
 var date = getDate(lastDate);
 return date.toLocaleString();
}
module.exports = {
 date: now,
 format: format
}
<!-- page/index/index.wxml -->
<wxs src="../dateTool.wxs" module="dateTool"></wxs>
<view>{{dateTool.date}}</view>

第三種,在一個wxs文件中引用另一個wxs文件

// /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" />

wxs語法和js很像,但是一定要注意,在外部寫完wxs文件后要給它的module對象中的exports屬性設(shè)置值

module.exports = { Key1:value1, key2: value2, };

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論