一、WXS的两种格式
wxs有点像我们html里面的script标签,既可以做内嵌式的写法,又可以做外链式的写法。
但是其中有模块化思想,所以,书写的函数需要导出才能使用。
1、WXS第一种写法
home.wxml中:
```html
<wxs module="md1">
// 只支持ES5写法,不支持ES6
function RMBFormater(val){
return "¥"+val.toFixed(2)+"元"
}
module.exports={
RMBFormater:RMBFormater
}
</wxs>
<view> {{md1.RMBFormater(20)}}</view>
<view> {{md1.RMBFormater(num1)}}</view>
<!-- 定义和调用没有先后顺序 -->
```
2、WXS第二种写法
home文件夹下新建home.wxs文件
```js
// 只支持ES5写法,不支持ES6
function RMBFormater(val){
return "¥"+val.toFixed(2)+"元"
}
console.log("这里的代码是会执行的");
module.exports={
RMBFormater:RMBFormater
}
```
home.wxml中:
```html
<view> {{md1.RMBFormater(20)}}</view>
<view> {{md1.RMBFormater(num1)}}</view>
<wxs src="./home.wxs" module="md1"></wxs>
```