gpt4 book ai didi

javascript - 登录后 Iron Router Route 中未定义 Meteor.userId()

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:11 25 4
gpt4 key购买 nike

当用户在未登录的情况下首次加载网站,然后登录时,下面定义的路由 account 将解析为 /profile/null。用户必须刷新站点才能使路线路径变得正确。

this.route('account', {
template: 'profile',
path: '/profile/' + Meteor.userId()
})

专门创建一个路由来获取参数Meteor.userId()的原因是因为我使用的是package这需要我定义 path 名称,即: {{> ionTab title="Account"path="account"}} 我认为它不能接受参数。

改进这条路线的更好方法是什么?

最佳答案

路由定义在您的应用程序启动时发生一次,此时 Meteor.userId() 仍未定义,因此这就是您的代码无法正常工作的原因,但整个方法是错误的,您应该相反,定义您的路线如下:

Router.route("/profile/:_id",{
name:"profile",
template:"profile",
waitOn:function(){
return Meteor.subscribe("userProfile",this.params._id);
},
data:function(){
var user=Meteor.users.findOne(this.params._id);
return {
user:user
};
}
});

您可能在 iron:router 文档中错过了定义采用参数(/path/:param 语法)的路由并使用此参数来配置路由订阅和数据上下文。

编辑

如果想获取该路由对应的动态路径,可以使用path方法:

HTML

<template name="myTemplate">
{{> ionTab title="Account" path=accountPath}}
</template>

JS

Template.myTemplate.helpers({
accountPath:function(){
return Router.path("profile",Meteor.user());
}
});

关于javascript - 登录后 Iron Router Route 中未定义 Meteor.userId(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724650/

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