2014-11-05 47 views
1

我是流星新手,使用javascript的經驗有限。我已經搜索了所有的帖子和網頁,瞭解如何使用Iron Router軟件包來路由靜態頁面的一個很好的教程,而且似乎無法弄清楚這一點。我希望有人幫助我瞭解如何爲Home and About設置router.js文件。我已經玩了很多代碼,但這是我在發佈之前所做的。在我看來,我似乎很難理解路由如何工作以及鐵路由器的所有各種功能,然後將這些路由連接到導航欄中,以便在它們之間導航。提前感謝您提供的任何幫助。靜態網站使用流星的路由器

客戶端/模板/應用/的layout.html

<template name="layout"> 
    <div class="container"> 
    {{> yield}} 
    </div> 
</template> 

的lib/router.js

Router.configure({ 
    layoutTemplate: 'layout' 
}); 

Router.route('/', function() { 
    this.render('home', { 
    template: 'home' 
    }); 

    this.render('about', { 
    template: 'about' 
    }); 
}); 

模板/ home.html的

<template name="home"> 
    <div class="container"> 
    <h2>Home</h2> 
    </div> 
</template> 
+0

http://meteortips.com/tutorial/iron-router-part-1/ – yoK0 2014-11-05 07:38:56

回答

1

的代碼你有以上內容正確。

一個怪癖是你爲你的/路線渲染兩頁。你應該有這樣的:

Router.route('/', function() { 
    this.render('home', {}); 
}); 

Router.route('/about', function() {  
    this.render('about', {}); 
}); 

記住this.render取第一個參數爲模板,所以沒有必要把它定義分開了。

和新about.html頁:

<template name="home"> 
    <div class="container"> 
     <h2>Home</h2> 
    </div> 
</template> 

現在你可以使用/和​​頁(至少我希望我沒有錯過什麼)

0

你可以有3個模板您的文件夾

客戶端/瀏覽次數

about.html 
main.html 
admin.html 
layout.html 
(for example) 

名稱,以便在about.html你有這樣的

<template name="about"> 
<h1> hello from about page 
</template> 

<template name="main"> 
<h1> hello from about page 
</template> 

<template name="admin"> 
<h1> hello from about page 
</template> 

中的layout.html文件需要CON玷污這個產量呈現。

<template name="layout"> 
    {{> yield}} 
    {{admin}} 
    {{about}} 
    {{main}} 
</template> 

所以,你可以使用佈局模板作爲母版頁並調用3個模板通過路由,如何分配路由分離,並告訴流星使用該佈局,以及使用該js代碼

JS

Router.configure({ 
    layoutTemplate: 'layout' 
}); 
Router.map(function(){ 
    this.route('admin', {path: '/admin'}); 
}); 
Router.map(function(){ 
    this.route('about', {path: '/about'}); 
}); 
Router.map(function(){ 
    this.route('main', {path: '/'}); 
}); 

至少,這對我的作品的兄弟,希望這對你的工作

+0

不是爲我工作。 – abhsss96 2015-10-22 20:02:33

相關問題