2015-10-12 162 views
0

我正在使用Iron:Router在Meteor JS的管理和客戶端門戶上工作。流星JS鐵路由器,路由子目錄

我知道我可以通過創建一個路由:

this.route('tasks',{path:'/projects', layoutTemplate: 'adminLayout'}); 

但有可能使一個子目錄,如路線:

this.route('tasks',{path:'/admin/projects', layoutTemplate: 'adminLayout'}); 

所以這樣我也可以有

this.route('/admin/projects', {name: 'admin.projects', template: 'projects', layoutTemplate: 'adminLayout'} 

:子目錄
this.route('/client/projects', {name: 'client.projects', template: 'projects', layoutTemplate: 'adminLayout'} 

感謝您的任何意見。

+0

那是完整的錯誤報告?看起來它只是錯誤的一半,如果不是,你可以發佈錯誤的完整錯誤或截圖。 – Sasikanth

+0

也嘗試更改路由的名稱,對於所有使用相同名稱'tasks'的路由,我認爲路由名稱應該不同 – Sasikanth

+0

嗨,我已經得到了錯誤消失,錯誤日誌實際上來自重複路由(管理員/客戶端)這就是爲什麼我想區分他們。但現在的問題是路線無法解決。 Iron:路由器識別路由,但不知道要加載的模板,從而使頁面處於加載狀態,只加載CSS。 –

回答

2

你擁有的所有路徑都可以在一個應用程序中很好地共存。 路由器(或您的瀏覽器)沒有任何目錄/子目錄的概念,它理解的只是字符串和正則表達式。嵌套純粹是我們(應該)創建的,使我們能夠理解app/api(/ codebase等)的結構。

由於Sasikanth指出,這不是完整的錯誤信息。但是看着packages/iron_middleware-stack/lib/middleware_stack.js line 31可以很容易地確認發生了什麼:

throw new Error("Handler with name '" + name + "' already exists."); 

這是Router.route功能,這是記錄here內。

Router.route('/post/:_id', { 
    // The name of the route. 
    // Used to reference the route in path helpers and to find a default template 
    // for the route if none is provided in the "template" option. If no name is 
    // provided, the router guesses a name based on the path '/post/:_id' 
    name: 'post.show', 

    // To support legacy versions of Iron.Router you can provide an explicit path 
    // as an option, in case the first parameter is actually a route name. 
    // However, it is recommended to provide the path as the first parameter of the 
    // route function. 
    path: '/post/:_id', 

    // If we want to provide a specific RouteController instead of an anonymous 
    // one we can do that here. See the Route Controller section for more info. 
    controller: 'CustomController', 

    // If the template name is different from the route name you can specify it 
    // explicitly here. 
    template: 'Post', 

    // and more options follow 

因此,對於上面包含的代碼,可以提供顯式路徑。因此,第一個參數是路由名稱。這些必須是唯一的,因爲它們用於查找pathFor,urlFor和linkTo助手中的路徑。由於您沒有提供明確的模板選項,因此該名稱也用於該選項,但您的代碼在獲取該選項之前會拋出此異常。

我想你想實現的是:

this.route('/projects', {name: 'projects', template: 'tasks', layoutTemplate: 'adminLayout'}); 
this.route('/admin/projects', {name: 'admin.projects', template: 'tasks', layoutTemplate: 'adminLayout'}); 
this.route('/client/projects', {name: 'client.projects', template: 'tasks', layoutTemplate: 'adminLayout'}); 
+0

嗨,請徹底disreagre控制檯錯誤,錯誤是從一個複製的路線沒有張貼,因爲我在前面的評論中提到。你是正確的,我試圖實現,我已經糾正,但應用程序卡在CSS加載後的加載階段。我已經將我的代碼更改爲:this.route('/ admin/projects',{name:'admin.projects',template:'projects',layoutTemplate:'adminLayout'} –

+0

您應該配置路由器以使用您的'路由器的佈局。配置(layoutTemplate:'adminLayout' });'也爲了簡單起見,現在刪除'name'屬性。考慮顛倒你的命名約定,也就是說它是'/ projects/admin'和'/ projects/client'。至於您遇到的實際問題,請確保您的所有項目文件中只有一個名爲'tasks'的模板,有時候重複會潛入並導致奇怪的錯誤。如果這沒有幫助,我們可能需要您將一些錯誤粘貼到控制檯或流星服務器中。 –

+0

也只是注意到,但你正在做'this.route'而不是'Router.route' - 我認爲這已被棄用了幾個版本 - 請參考[Iron-Router]官方入門指南(http:// iron-meteor.github.io/iron-router/) –