【发布时间】:2016-02-11 00:57:27
【问题描述】:
我开始学习流星,并设置了一个小型测试项目。我做了一个像 test.html 这样的简单模板:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Test</title>
</head>
<body>
{{> TestPage}}
</body>
<template name="TestPage">
<p>hello</p>
</template>
我还安装了iron-router,并尝试设置了路由。然后我检查了meteor run,一切正常,我得到了一个带有hello 文字标志的普通页面。之后我想尝试制作简单的登录页面,我添加了meteor add accounts-ui accounts-password。之后,当我尝试加载我的页面时,hello 显示了两次。所以我去了js部分尝试了解发生了什么。这是完整的 test.js:
var textMessage = "";
var textMessageDep = new Deps.Dependency;
var getTextMessage = function()
{
textMessageDep.depend();
return textMessage;
};
var setTextMessage = function (newValue) {
if (newValue !== textMessage)
textMessageDep.changed();
textMessage = newValue;
};
Template.TestPage.onRendered(function () {
console.log('on rendered');
});
Template.TestPage.onCreated(function () {
console.log('on created');
});
因此,我设法在 js 控制台中获得以下输出:
on created
on created
on rendered
On created 事件由于某种原因被调用了两次,这就是为什么我得到了两个模板副本。如何正确克服这个问题?我假设流星等待Meteor.Users 集合准备好?我什至没有将任何用户添加到此集合中。
这也是我的路由器配置的样子:
Router.map(function() {
this.route('/', {
onBeforeAction: function () {
// here planned to add
// if(!Meteor.user()) {}
//
this.render('TestPage');
}
});
});
【问题讨论】:
标签: javascript node.js meteor meteor-accounts accounts