收藏
回答

模板is中的Mustache语法能改成函数吗?

框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本
小程序 Bug template 客户端 6.6.6 2.0.5

我定义了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 个回答

  • 黄思程
    黄思程
    2018-05-11

    要使用函数可参考 wxs 用法

    2018-05-11
    有用
    回复
  • Jacky
    Jacky
    2018-05-30

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

    2018-05-30
    有用
    回复
  • 疯狂的小辣椒
    疯狂的小辣椒
    2018-05-11

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

    2018-05-11
    有用
    回复
登录 后发表内容