模板is中的Mustache语法能改成函数吗?
发布于 5 年前 作者 tao42 8801 次浏览 来自 问答

我定义了textTmpl、numberTmpl、radioTmpl、checkboxTmpl共4个template,根据item.type来匹配模板的name,目前按【方式1】写是可以正常渲染,但是按【方式2】无法正常渲染

方式1:

<block wx:for="{{questions}}" wx:key=“id”>

<template is="{{item.type == ‘TEXT’ ? ‘textTmpl’ : ( item.type == ‘NUMBER’ ? ‘numberTmpl’ : ( item.type == ‘RADIO’ ? ‘radioTmpl’ : (item.type == ‘CHECKBOX’ ? ‘checkboxTmpl’ : ‘default’)))}}" data="{{…item}}" />

</block>

方式2:

<block wx:for="{{questions}}" wx:key=“id”>

<template is="{{getIs(item)}}" data="{{…item}}" />

</block>

page中定义了函数getIs,虽然是照搬,但实际的判断逻辑不止根据item.type匹配,还会有别的字段校验逻辑

getIs:function(item){

console.log(item.type)

return item.type == ‘TEXT’ ? ‘textTmpl’ : (item.type == ‘NUMBER’ ? ‘numberTmpl’ : (item.type == ‘RADIO’ ? ‘radioTmpl’ : (item.type == ‘CHECKBOX’ ? ‘checkboxTmpl’ : ‘default’)))

}

微信开发工具的报错信息:Template “undefined” not found.

3 回复

你好,这个只能按照方式一来写的

要使用函数可参考 wxs 用法

该问题已曲线解决,谢谢各位

回到顶部