【问题标题】:Unexpected token while compiling ejs i18next编译 ejs i18next 时出现意外的令牌
【发布时间】:2019-06-17 04:21:22
【问题描述】:

我在使用 i18next 进行翻译时收到错误 Unexpected token ) while compiling ejs

我已经用i18next实现了翻译,下面是如下代码,使用map函数时出现如下错误

en.json

{
  "title": "List of Countries",
  "list":[{
     "name": "Singapore",
     "code": "SG"
  },{
    "name": "Thailand",
    "code": "TH"
 }]
}

index.ejs 
  <h4><%=t('title')%></h4> //outputs correctly
  <ul>
      <%=t('list').map(e=>{%>
          <li><%=e.name%></li>
      <%})%>
  </ul>

【问题讨论】:

    标签: javascript html node.js ejs i18next


    【解决方案1】:

    只需删除分配字符就可以了。

    <ul>
        <% t.list.map(e => {%>
           <li><%=e.name%></li>
        <%})%>
    </ul>
    

    【讨论】:

      【解决方案2】:

      您的 ejs 脚本在遍历数组时不正确,您需要在迭代时删除分配字符并使用对象“e”而不是“li”,如下所示:

      <ul> 
      <%_ t('list').map(e=>{-%> 
      <li><%=e.name%></li>
      <%_  }) -%>
      </ul>
      

      用途:

      "

      "-%>" 删除行

      查看official ejs docs了解更多详情

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        • 2022-10-21
        • 1970-01-01
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多