使用流星,我试图使用模板作用域中一个文件中的功能位于另一个文件中。我尝试使用箭头功能:
第一个文件:
export const myFunc = ()=>{
console.log(this.x);
};
第二个文件:
import {myFunc} from './myFunc.js';
Template.MyTemplate.onCreated(function(){
this.x = 4;
myFunc(); //undefined
});
使用模板本身未定义的函数来影响模板变量的最佳方法是什么?(我也需要其他模板的那些功能)
将模板的变量作为参数传递给函数呢?结合使用ReactiveVar,您可以在myFunc中进行设置
import { myFund } from './myFunc.js'
Template.MyTemplate.onCreated(function () {
this.myVar = new ReactiveVar('Foo');
});
Template.myTemplate.onRendered(function() {
myFunc(this.myVar);
console.log(this.myVar.get()) // 'Bar'
});
并在您的功能文件中
export const myFunc = (myVar) => {
//Use myVar
myVar.set('Bar')
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句