[英]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.