2016-05-15 106 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>