Javascript 在Riot.js表达式中调用全局函数

Javascript 在Riot.js表达式中调用全局函数,javascript,riot.js,Javascript,Riot.js,我试图从Riot.js中的表达式调用在全局命名空间中声明的函数 这不起作用: <strong>Created { getDateString(item.created) } by { item.creator }</strong> 包含此函数的整个JavaScript文件已加载。。。如果我从this.on('mount')调用getDateString(),它可以工作: this.on('mount', function() { getDateString(ne

我试图从Riot.js中的表达式调用在全局命名空间中声明的函数

这不起作用:

<strong>Created { getDateString(item.created) } by { item.creator }</strong>
包含此函数的整个JavaScript文件已加载。。。如果我从
this.on('mount')
调用getDateString(),它可以工作:

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

我真的不理解Riot.js中的名称空间是如何工作的,因此我无法理解为什么对getDateString()的调用在表达式中失败,但在mount函数中成功。有人能告诉我我做错了什么吗?

确保您的
globalFunction()
是在global中声明的。标记定义中的
标记的范围不是全局的。小心点

<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>

{someGlobalFunction(message)}

{localFunction1(消息)}

{localFunction2(消息)}

{localFunction3(消息)}

this.message='world' //无法从模板访问 函数localFunction1(arg){ 返回“Hello”+arg+“!” } //可访问,因为这与localFunction3相同 localFunction2(arg){ 返回“Hello”+arg+“!” } //可从模板访问 this.localFunction3=函数(arg){ 返回“Hello”+arg+“!” }.绑定(此)
确保您的
globalFunction()
已在全局中声明。标记定义中的
标记的范围不是全局的。小心点

<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>

{someGlobalFunction(message)}

{localFunction1(消息)}

{localFunction2(消息)}

{localFunction3(消息)}

this.message='world' //无法从模板访问 函数localFunction1(arg){ 返回“Hello”+arg+“!” } //可访问,因为这与localFunction3相同 localFunction2(arg){ 返回“Hello”+arg+“!” } //可从模板访问 this.localFunction3=函数(arg){ 返回“Hello”+arg+“!” }.绑定(此)
是否在全局位置定义了
getDateString()
?看看这个例子。它很好用。结果是我试图传递一个不存在的变量,所以函数没有被调用,因为JS出错了。一旦我解决了这个问题,正确的函数就被调用了。我通常会尝试将我在代码html部分中使用的所有东西都放在标记的本地,或者创建一个调用其他东西的函数,或者使用mixin。从长远来看,它会使代码工作得更好,因为它使所有复杂的逻辑都成为“普通的javascript”,而且我永远不必猜测6个月后我调用的函数可能在哪里。
getDateString()
实际上是在global定义的吗?看看这个例子。它很好用。结果是我试图传递一个不存在的变量,所以函数没有被调用,因为JS出错了。一旦我解决了这个问题,正确的函数就被调用了。我通常会尝试将我在代码html部分中使用的所有东西都放在标记的本地,或者创建一个调用其他东西的函数,或者使用mixin。从长远来看,它会使代码工作得更好,因为它使所有复杂的逻辑都成为“普通的javascript”,而且我永远不必猜测6个月后我调用的函数可能在哪里。这是一个很好的澄清。原来这不是我的问题,但这肯定有助于澄清我看到的其他一些事情。谢谢复制/粘贴错误,我传递了一个不存在的变量,Javascript不知道怎么做。一旦我更改了参数,函数就按预期调用了。这是一个很好的澄清。原来这不是我的问题,但这肯定有助于澄清我看到的其他一些事情。谢谢复制/粘贴错误,我传递了一个不存在的变量,Javascript不知道怎么做。一旦我更改了参数,函数就会按预期调用。