[英]Calling global function in Riot.js expression
I am trying to call a function declared in the global namespace from an expression in Riot.js. 我试图从Riot.js中的表达式调用在全局名称空间中声明的函数。
This does not work: 这不起作用:
<strong>Created { getDateString(item.created) } by { item.creator }</strong>
I can call the global moment()
function (from moment.js): 我可以调用全局
moment()
函数(来自moment.js):
<strong>Created { moment(item.created) } by { item.creator }</strong>
The overall JavaScript file containing this function is loaded... if I call getDateString() from this.on('mount')
it works: 包含此功能的总的JavaScript 文件加载...如果我从调用getDateString()
this.on('mount')
它的工作原理:
this.on('mount', function() {
getDateString(new Date());
});
I don't really understand how namespacing works in Riot.js, and so I can't figure out why my call to getDateString() is failing in the expression but succeeding in the mount function. 我不太了解Riot.js中的命名空间是如何工作的,因此我无法弄清楚为什么对getDateString()的调用在表达式中失败但在mount函数中成功。 Can someone tell me what I'm doing wrong?
有人可以告诉我我在做什么错吗?
Make sure your globalFunction()
is declared at global. 确保您的
globalFunction()
在全局声明。 The scope of <script>
tag inside the tag definition is not global. 标签定义中
<script>
标签的范围不是全局的。 Take care about it. 保重
<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.