简体   繁体   English

流星流路由器设置example.com/singlePostPage

[英]Meteor Flow Router setting up example.com/singlePostPage

I can not manage to create the routes to show a single post with flowrouter and blaze in Meteor. 我无法创建路线以显示带有flowrouter的单个帖子并在Meteor中大放异彩。

This is what I have so far which I am sure its mostly wrong! 到目前为止,这是我所知道的,我确信这主要是错误的!

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>

I used to do it back then with Iron router but now got confused with Flow router. 那时我曾经使用Iron路由器来做,但是现在却与Flow路由器混淆了。

First don't use the FlowRouter subscriptions. 首先,不要使用FlowRouter订阅。 That will soon be deprecated. 那将很快被弃用。 Use Meteor PubSub. 使用流星PubSub。 First in the routes.js: 首先是routes.js:

    // http://app.com/posts/:_id
    FlowRouter.route('/posts/:id', {
        name: "postPage",
        action: function(params, queryParams) {
            BlazeLayout.render("nav", {yield: "postPage"} )
        }
    });

Then when the template is created you subscribe using Meteor's subscription: 然后,在创建模板后,您可以使用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
    });
});

Then the helper will be: 然后该助手将是:

// 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;
    }
});

You also need to check if the subscriptions are ready in html. 您还需要检查html是否已准备好订阅。

{{#if Template.subscriptionsReady}}
    {{#with thisPost}}
        <li>{{title}}</li>
    {{/with}}
{{else}}
    <p>nothing to show</p>
{{/if}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Meteor.js 部署到“example.com”还是“www.example.com”? - Meteor.js deploy to "example.com" or "www.example.com"? 流路由器流星的历史 - History with flow router meteor Safari 将 vcf 下载为 example.com javascript - Safari downloads vcf as example.com javascript 在example.com中使用setcookie(),在www.example.com中找不到cookie - setcookie() in example.com, cookie not found in www.example.com “if(location.href!= example.com/page1 || example.com/page2)”更改CSS元素 - “if (location.href != example.com/page1 || example.com/page2)” Change CSS element 使用Flow Router,Meteor和React导航 - Navigation with Flow Router, Meteor and React 在 Meteor 中使用 Flow Router 对路由进行分组 - Grouping routes with Flow Router in Meteor 将example.com路由到静态文件,但将example.com/anything路由到Express-JS中的其他文件 - Routing example.com to a static file but example.com/anything to something else in Express-JS 如何将portnumber(example.com:1234)更改为(example.com)? - How can I change the portnumber (example.com:1234) to something like (example.com)? 内容安全策略标头的 *.example.com 是否也与 example.com 匹配? - Does a *.example.com for a content security policy header also match example.com?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM