我试图从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()的调用在表达式中失败,而在挂载函数中成功。有人能告诉我我做错了什么吗?
发布于 2015-08-31 21:12:04
确保您的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>https://stackoverflow.com/questions/32299073
复制相似问题