Meteor ReactiveVar 无法设置值
2018-07-21
467
请问该怎么做?我正在尝试使用 ReactiveVar 设置占位符的值,但一直出现此错误。它显示 ReactivarVar 无法看到它的设置函数。
这是错误
Uncaught TypeError: Cannot read property 'set' of undefined
at Object.change #identity (login.js:19)
at blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3775
at Function.Template._withTemplateInstanceFunc (blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744)
at Blaze.View.<anonymous> (blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3774)
at blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2617
at Object.Blaze._withCurrentView (blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2271)
at Blaze._DOMRange.<anonymous> (blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2616)
at HTMLSelectElement.<anonymous> (blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:863)
at HTMLDivElement.dispatch (jquery.js?hash=e15504227515793c40a6a3a8ad340399cb657500:5282)
at HTMLDivElement.elemData.handle (jquery.js?hash=e15504227515793c40a6a3a8ad340399cb657500:4934)
这是代码
Template.login.onCreated(() => {
this.identify = new ReactiveVar( "" );
});
Template.login.helpers({
identify() {
return Template.instance().identify.get();
}
})
Template.login.events({
'change #identity'(event, template){
event.preventDefault();
if ($(event.target).val() == "one") {
console.log("Enter Phone Number");
template.identify.set( "Enter Phone Number" );
}
if ($(event.target).val() == "two") {
console.log("Enter Phone Number");
template.identify.set( "Enter Age" );
}
if ($(event.target).val() == "one") {
console.log("Enter Phone Number");
template.identify.set( "Enter ID Card Number" );
}
}
});
这是我的模板文件
<form class="startup" id="startup">
<select name="identity" style="width: 310px; height: 40px;" id="identity" >
<option selected value="select">Select an option</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input type="text" name="identify" id="identify" required="true" placeholder= {{identify}} >
<input type="password" name="Password" placeholder="Password" required="true">
<input type="submit" value="Sign Up">
</form>
2个回答
您需要将实际的 ReactiveVar 设置为一个命名变量。
例如
this.identify = new ReactiveVar( "state" );
现在您可以通过这种方式访问​​它。
设置变量(在事件中)
temp.identify.state.set("age", 28);
获取变量(在帮助程序中)
Template.identify.state.get("age")
Aman
2018-08-13
您可以使用这个 ReactiveVar 包装器。它干净且易于使用。请找到这个有用的链接: https://github.com/frozeman/meteor-template-var
manish
2018-07-30