2015-04-19 186 views
1

當用戶第一次登錄時加載站點,然後登錄,下面定義的路由account將解析爲/profile/null。用戶必須在路徑的路徑正確之前刷新網站。登錄後在鐵路由器路由中未定義的Meteor.userId()

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

之所以專門創建的路線來走參數Meteor.userId()是因爲我使用的是package,需要我定義path名,即:{{> ionTab title="Account" path="account"}}我不認爲可以採取的參數。

最好的方法來完善這條路線?當你的應用程序啓動

回答

1

路由定義發生一次,此時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()); 
    } 
}); 
+0

謝謝,我正在使用[package](https://github.com/meteoric/meteor-ionic),它需要我定義路徑名,例如:{{> ionTab title =「Account」path =「account」}}'我不認爲可以帶參數 – Nyxynyx

+0

檢查我的編輯,你可以得到動態計算的路由路徑參數。路由參數名稱應該與作爲第二個參數傳遞給'Router.path'的對象的屬性匹配。 – saimeunt

+0

當我使用'Router.path('profile',{'user_id':Meteor.userId()})'時,在瀏覽器JS控制檯中得到了正確的路徑。使用'Router.path(「profile」,Meteor.user());'雖然給了一個'null'... – Nyxynyx