当前位置:网站首页>在小程序的 wxml 文件中使用 js 函数
在小程序的 wxml 文件中使用 js 函数
2022-06-21 20:59:00 【代码搬运媛】
在小程序的开发过程中,有时候会用到直接在 wxml 中写 js 方法,比如处理数据:保留两位小数。这样如果在 js 文件中直接处理后台返回数据会比较繁琐,所以就用到了 wxs 文件。
wxs可以说就是为了满足能在页面中使用js存在的,在wxml页面中,只能在插值{ { }}中写简单的js表达式,而不能调用方法,
<view class="coupon-name">{
{ sub(couponItem.couponName,12) }}</view>
上边这种写法不生效,在小程序中是不支持这种语法的。
解决方法
(1)需要创建一个wxs文件
// sub.wxs
var sub = function (str, length) {
return str && str.slice(0, length)
}
module.exports.sub = sub;
(2)在wxml文件中引入该文件,并调用你想要用到的函数
<wxs src="./sub.wxs" module="tools" />
...
...
<view class="coupon-name">{
{ tools.sub(couponItem.couponName,12) }}</view>
边栏推荐
- WPF 选择文件夹
- Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition
- Correspondence between rtx3090 and pytorch versions
- 必读书籍
- About Eureka starting successfully but accessing 404
- Fedora 36 compiling and installing opencv 4.6.0 -- the road to building a dream
- numpy矩阵初等变换
- 啊啊啊啊啊啊啊
- FPGA之道——FPGA开发流程之项目方案与FPGA设计方案
- class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist
猜你喜欢

Second understanding microservice

关于lg(n!)的渐进紧确界

解决opencv在pycharm中代码提示失效

WPF routing

WPF 路由
Swiftui basic learning journal (XI) SQLite data operation

What is the most challenging issue in Bi development?

阿里出品!图形化的ant脚本——IDEA插件CloudToolkit

Analysis of 43 cases of MATLAB neural network: Chapter 19 handwritten font recognition based on SVM

Technology sharing | introduction to kubernetes pod
随机推荐
WPF listbox virtualization
C delete files in use
WPF routing
UWP Dispatcher用法
左手代码,右手开源,开源路上的一份子
Games101 job 7- detailed explanation of implementation steps of multi thread speed up
软件测试----测试的分类
MySQL pit records
啊啊啊啊啊啊啊
Resolve the invalidation of OpenCV code prompt in pycharm
【强烈推荐】Markdown 语法大全
必讀書籍
FPGA设计中RAM和ROM初始化的方法
UWP 手写板InkCanvas
KVM虚拟机在线扩展磁盘 —— 筑梦之路
WPF 线程操纵UI问题
Left hand code, right hand open source, part of the open source road
个人的股票交易经验
Nacos installation guide
.bmp图片的文件头解析