我无法创建路线以显示单个带有flowrouter的帖子并在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>


那时我曾经使用Iron路由器来做,但是现在却与Flow路由器混淆了。

最佳答案

首先,不要使用FlowRouter订阅。那将很快被弃用。使用流星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}}

关于javascript - meteor 流路由器设置example.com/singlePostPage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34583383/

10-16 22:00