2016-05-15 103 views
0

通過鐵路由器發送數據上下文模板這是我的路由器:不能流星

Router.route('/cource/:courcePath', { 
    name: 'Cource_page', 
    template: 'Cource_page', 
    data() { 
    return Cources.find({ courcePath: this.params.courcePath }); 
    } 
}); 

模板:

<template name="Cource_page"> 
    <div class="container"> 
    {{#each cources}} 
     <h1>{{courceTitle}}</h1> 
    {{/each}} 
    </div> 
</template> 

和助手:

Template.Cource_page.helpers({ 
    cources() { 
    let courcePath = this.courcePath; 
    console.log(courcePath); 
    return Cources.find({ courcePath: courcePath }); 
    } 
}); 

當我去到某個頁面(例如http://localhost:3000/cource/my-new-cource),沒有數據呈現,我在控制檯中獲得undefined(模板h的輸出elpers)。我究竟做錯了什麼?

回答

0

你需要傳遞在渲染指令數據

Router.route('/post/:_id', function() { 
    this.render('Post', { 
    data: function() { 
     return Posts.findOne({_id: this.params._id}); 
    } 
    }); 
}); 

OR

你應該訂閱的路由數據:

Router.route('/cource/:courcePath', { 
    name: 'Cource_page', 
    template: 'Cource_page', 
    subscriptions: function() { 
    this.subscribe('courses', this.params.courcePath); 
    } 
}); 

檢查更多的信息指南:

http://iron-meteor.github.io/iron-router/

+0

PS:我建議你學習Flow-Router,它正在成爲Meteor的事實上的路由器,並且更加靈活。我在Iron-Router上構建了自己的應用程序,並且在開始與Iron-Router發生問題時不得不重寫很多內容(如隨機重新渲染...)https://github.com/kadirahq/flow-router – MrE

0

當您在i-r路由中提供數據上下文時,您不需要幫助程序。嘗試:

Router.route('/cource/:courcePath', { 
    name: 'Cource_page', 
    template: 'Cource_page', 
    data() { 
    return Cources.find({ courcePath: this.params.courcePath }); 
    } 
}); 

<template name="Cource_page"> 
    <div class="container"> 
    {{#each this}} 
     <h1>{{courceTitle}}</h1> 
    {{/each}} 
    </div> 
</template>