[英]Meteor - Page refresh causes flicker when fetching data for router
If I refresh a meteor page that is using a data query in the iron-router setup, the page loads the template but with no data, then shows the loading template, then shows the page with the data. 如果刷新在流星路由器设置中使用数据查询的流星页面,则该页面将加载模板但不包含数据,然后显示正在加载的模板,然后显示包含数据的页面。 I want to avoid the page flicker that is happening. 我想避免发生页面闪烁。 This is my router code: 这是我的路由器代码:
this.route('/stories/:_id', function () {
if (!Meteor.user() && !Meteor.loggingIn()) {
Router.go('home');
} else {
var story = Stories.findOne({ _id: this.params._id });
this.render('showStory', { data: story });
}
});
I've also tried this setup and moved the logged in validation to an onBeforeAction. 我还尝试了此设置,并将已登录的验证移至onBeforeAction。
this.route('showStory', {
path: '/stories/:_id',
data: function () {
return Stories.findOne({ _id: this.params._id });
}
});
When I refresh the page with this setup, I see my 404 page, then the loading template, then the correct template with data. 使用此设置刷新页面时,会看到404页面,然后是加载模板,然后是包含数据的正确模板。
Try with this. 试试这个。
Router.map(function () {
this.route('showStories', {
path: '/stories/:_id',
waitOn: function(){
return Meteor.subscribe("Stories"); //we make de subscription here
},
data: function(){
if(! Meteor.user()){
this.render('/') //or sign-up template whatever template you want if user its not loged in
}else{
return Stories.findOne({_id: this.params._id});
}
}
});
});
Already Tested and its working 已经过测试及其工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.