Javascript Meteor Flow路由器设置示例.com/singlePostPage

Javascript Meteor Flow路由器设置示例.com/singlePostPage,javascript,meteor,meteor-blaze,spacebars,flow-router,Javascript,Meteor,Meteor Blaze,Spacebars,Flow Router,我无法通过flowrouter和blaze在Meteor中创建显示单个帖子的路线 到目前为止,这就是我所知道的,我确信这基本上是错的 publications.js Meteor.publish('singlePost', function (postId) { return Posts.find({ _id: postId }); }); Router.js FlowRouter.route("/posts/:_id", { name: "postPage", subsc

我无法通过flowrouter和blaze在Meteor中创建显示单个帖子的路线

到目前为止,这就是我所知道的,我确信这基本上是错的

publications.js

Meteor.publish('singlePost', function (postId) {
  return Posts.find({ _id: postId });
});
Router.js

FlowRouter.route("/posts/:_id", {
    name: "postPage",
    subscriptions: function (params, queryParams) {
     this.register('postPage', Meteor.subscribe('singlePost'));
 },
    action: function(params, queryParams) {
        BlazeLayout.render("nav", {yield: "postPage"} )
    }
});
singlePost.JS

Template.postPage.helpers({
  thisPost: function(){
    return Posts.findOne();
  }
});
singlePost.html

<template name="postPage">
  {{#with thisPost}}
    <li>{{title}}</li>
  {{/with}}
</template>

{{{#用这篇文章}
  • {{title}}
  • {{/与}}

    我以前用Iron router做这个,但现在把它和Flow router搞混了。

    首先不要使用Flow router订阅。这将很快被弃用。使用Meteor PubSub。首先在routes.js中:

        // http://app.com/posts/:_id
        FlowRouter.route('/posts/:id', {
            name: "postPage",
            action: function(params, queryParams) {
                BlazeLayout.render("nav", {yield: "postPage"} )
            }
        });
    
    创建模板后,您可以使用Meteor的订阅进行订阅:

    // Template onCreated
    Template.postPage.onCreated(function() {
        // Subscribe only the relevant subscription to this page
        var self = this;
        self.autorun(function() { // Stops all current subscriptions
            var id = FlowRouter.getParam('id'); // Get the collection id from the route parameter
            self.subscribe('singlePost', id); // Subscribe to the single entry in the collection with the route params id
        });
    });
    
    那么助手将是:

    // Template helper functions
    Template.postPage.helpers({
        thisPost: function() {
            // Get the single entry from the collection with the route params id
            var id = FlowRouter.getParam('id');
            var post = Posts.findOne({ // Get the selected entry data from the collection with the given id.
                _id: id
            }) || {};
            return post;
        }
    });
    
    您还需要检查订阅是否已在html中就绪

    {{#if Template.subscriptionsReady}}
        {{#with thisPost}}
            <li>{{title}}</li>
        {{/with}}
    {{else}}
        <p>nothing to show</p>
    {{/if}}
    
    {{#if Template.subscriptionsReady}
    {{{#用这篇文章}
    
  • {{title}}
  • {{/与}} {{else} 没什么可展示的

    {{/if}
    谢谢,但还是不行……发布功能如何知道什么是postId?我没有在任何地方定义它…我实际上是从其他地方复制代码的某些部分以使其工作,但我认为这是我最困惑的部分…谢谢,我正在编辑我的评论,因为我注意到了其他事情。别担心,我也有这个问题。等一下;)感谢您的详细回复和评论!它现在工作得很好!我将开始学习所有这些,因为它对于每个应用程序都是强制性的;)祝你过得愉快没问题;)实际上,我建议您将此作为Meteor应用程序样板文件的一部分。这是我写的:)我只需要把事件转换成帖子,就是这样。