我定义了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.