簡體   English   中英

在Riot.js表達式中調用全局函數

[英]Calling global function in Riot.js expression

我試圖從Riot.js中的表達式調用在全局名稱空間中聲明的函數。

這不起作用:

<strong>Created { getDateString(item.created) } by { item.creator }</strong>

可以調用全局moment()函數(來自moment.js):

<strong>Created { moment(item.created) } by { item.creator }</strong>

包含此功能的總的JavaScript 文件加載...如果我從調用getDateString() this.on('mount')它的工作原理:

this.on('mount', function() {
    getDateString(new Date());
});

我不太了解Riot.js中的命名空間是如何工作的,因此我無法弄清楚為什么對getDateString()的調用在表達式中失敗但在mount函數中成功。 有人可以告訴我我在做什么錯嗎?

確保您的globalFunction()在全局聲明。 標簽定義中<script>標簽的范圍不是全局的。 保重

<my-tag>
  <p>{ someGlobalFunction(message) }</p><!-- will work -->
  <p>{ localFunction1(message) }</p><!-- won't work -->
  <p>{ localFunction2(message) }</p><!-- will work -->
  <p>{ localFunction3(message) }</p><!-- will work -->

  <script>
    this.message = 'world'

    // Not reachable from the template
    function localFunction1 (arg) {
      return 'Hello ' + arg + '!'
    }

    // Reachable because this is the same as localFunction3
    localFunction2 (arg) {
      return 'Hello ' + arg + '!'
    }

    // Reachable from the template
    this.localFunction3 = function(arg) {
      return 'Hello ' + arg + '!'
    }.bind(this)
  </script>
</my-tag>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM