wx:for双引号转义的问题
发布于 6 年前 作者 baiyong 8840 次浏览 来自 问答

JS:

Page({
  data: {
    etymon: {
      spelling: "trans-",
      meaning: "横向;通过;超过",
      evolution: "本词根意味着 \"across, beyond, through, on the other side of, to go beyond\",来自拉丁语中的trans (prep.) \"across, over, beyond\",很可能源自 trare- 的分词结构 \"to cross\", 始于原始印欧语中的 *tra-,即*tere的变体 \"cross over, pass through, overcome.\" \n在化学中,这个词根用来表示一个化合物中的两个官能团位于分子中轴的对侧。"
    }
  }
})

WXML:

<text wx:for="{{etymon.evolution.split("\n")}}" wx:key="unique" class="paragraph">
  {{item}}
</text>

wx:for 里面是个长字符串,有换行符号\n,想用split 分解成一个数组,但是这里似乎有转义的问题。

试过:

<text wx:for="{{etymon.evolution.split("\n")}}" wx:key="unique" class="paragraph">

会报错:Uncaught SyntaxError: missing ) after argument list

如果这样:

<text wx:for="{{etymon.evolution.split(\"\\n\")}}" wx:key="unique" class="paragraph">
  {{item}}
</text>

确实没有报错,但是页面上一片空白

少写一个\ 也会报错

<text wx:for="{{etymon.evolution.split(\"\n\")}}" wx:key="unique" class="paragraph">
  {{item}}
</text>

报错信息:Bad attr `wx

到底这个语法要怎么写才对呢?

3 回复

用wxs写个方法

在 js 里先处理好数据 etymon.evolution.split("\n"

然后在wx:for里直接给

wxml 里只支持data里的数据,不支持函数


应该是标签里不支持split方法

回到顶部