gpt4 book ai didi

javascript - 一秒弹出未找到页面-Meteor-iron-router

转载 作者:行者123 更新时间:2023-11-29 15:37:09 26 4
gpt4 key购买 nike

我将我的应用程序升级到 Meteor 1.0 并更新了我的 router.js,因为我不能使用 .wait() anymore .但是,现在我未找到的页面只弹出一秒钟,然后才出现“真实页面”。我该如何解决这个问题?

这是我的代码:

this.route('gamePage', {
path: '/game/:slug/',
onBeforeAction: [function() {
this.subscribe('singlePlayer', this.params.slug);
var singlePlayer = this.data();
if (singlePlayer) {
if (singlePlayer.upgrade) {
this.subscribe('upgrades', this.params.slug);
}
}
this.next();
}],
data: function() {
return Games.findOne({slug: this.params.slug});
},
waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});

如有任何帮助,我们将不胜感激。

最佳答案

尝试改用订阅模式。

this.route('gamePage', {
path: '/game/:slug/',
subscriptions: function() {
return Meteor.subscribe('singlePlayer', this.params.slug);
},
onBeforeAction: function() {
var singlePlayer = this.data();
if (singlePlayer) {
if (singlePlayer.upgrade) {
this.subscribe('upgrades', this.params.slug);
}
}
this.next();
},
data: function() {
return Games.findOne({slug: this.params.slug});
},
waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});

但是,重要的是您还包括 loading 插件以利用 loadingTemplate

Router.configure({
loadingTemplate: 'loading' // general purpose loading template
});

// built in plugin.. surprisingly not clearly specified in current docs, but you can dive in the code for plugins.
// https://github.com/EventedMind/iron-router/blob/devel/lib/plugins.js

Router.onBeforeAction('loading', { only: ['gamePage'] }); // only show loading for pages with subscriptions

Router.map(function() {
this.route('gamePage',{
//... your other options here ..
loadingTemplate: 'gamePageLoading', // game Page dedicated loading markup.
});
});

如果您想留在 onBeforeAction 实现中,还有 this.ready() 模式。

this.route('gamePage', {
path: '/game/:slug/',
onBeforeAction: [function() {
this.subscribe('singlePlayer', this.params.slug);

if(this.ready()) {
var singlePlayer = this.data();
if (singlePlayer) {
if (singlePlayer.upgrade) {
this.subscribe('upgrades', this.params.slug);
}
}
this.next();
} else {
this.render('loading');
}

}],
data: function() {
return Games.findOne({slug: this.params.slug});
},
waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});

来源:https://github.com/EventedMind/iron-router/blob/devel/Guide.md#subscriptions

我认为此更改是必要的,因为 .wait 模式被视为不必要的链接并且容易出现(编码)错误。此外,在覆盖 onBeforeAction 时显式处理 .next() 现在可确保此 Hook (如果不是所有其他 Hook ,也可能是大多数)的正确计时。

关于javascript - 一秒弹出未找到页面-Meteor-iron-router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com