繁体   English   中英

流星铁路由器动态网段不起作用

[英]meteor iron router dynamic segment not working

我一直在研究流星,这太棒了! 我一直在寻找路由解决方案,但是我发现'iron-router'很酷,我设法使get static页面与模板一起使用,但是当我转到/ posts / 123时,该页面无法呈现。 我一直在这个网站上观看视频

https://www.eventedmind.com/feed/q8QWX5e7PTu8BEReY

这是我的代码

blog.js编辑

Posts = new Meteor.Collection('posts');

Router.configure({
  layout: 'layout',
  loadingTemplate: 'loading',
  notFoundtemplate: 'notFound'
});

Router.map( function (){
  this.route('posts', {
    path: '/',
    waitOn: function () {
      return App.subs.posts;
    },
    data: {
      posts: function () {
        return Posts.find({}, {sort: {order: 1}});
      }
    }
  });

  this.route('postShow', {
    path: '/posts/:_id'
  });
});

if (Meteor.isServer) {
  Meteor.publish('posts', function () {
    return Posts.find({}, {sort: {order: 1}});
  });

  Meteor.publish('post', function (id) {
    return Posts.find({_id: id});
  });
}

if (Meteor.isClient) {
  App = {
    subs: {
      posts: Meteor.subscribe('posts')
    }
  };

  PostShowController = RouteController.extend({
    template: 'postShow',
    before: function () {
      var _id = this.params._id;

      if(App.subs.post)
        App.subs.post.stop();

      App.subs.post = Meteor.subscribe('post', _id);
    },
    data: {
      body: function () {
        return Posts.findOne({_id: this.params._id});
      }
    },
    run: function () {
      this.render('postShow');
    }
  });
}

blog.html

<head>
    <title>IronRouter</title>
</head>

<body>
</body>

<template name="layout">
    <div class="container">
        <aside class="sidebar">
            <div class="sidebar-inner">
                {{yield 'sidebar'}}
            </div>
        </aside>

        <section class="content">
            <div class="content-inner">
                {{yield}}
            </div>
        </section>
    </div>
</template>

<template name="notFound">
  <h1>Not Found</h1>
</template>

<template name="loading">
  <h1>Loading...</h1>
</template>

<template name="posts">
  <h1>Posts</h1>
  <ul>
    {{#each posts}}
    <li>{{> postListItem}}</li>
    {{/each}}
  </ul>
</template>

<template name="postListItem">
    <a href="{{pathFor 'postShow'}}">
        {{title}}
    </a>
</template>

<template name="postShow">
  <p>
    {{body}}
  </p>
</template>

<template name="postShowSidebar">
  <h1>{{title}}</h1>
</template>

我是视频的作者,可能有点过时了。 我很抱歉。 我将执行以下操作:

  1. layout: 'layout'更改为layoutTemplate: 'layout'
  2. 从控制器中删除run方法。 该方法实际上起了很多作用,因此,如果要覆盖渲染,可以改用action方法。 但就您而言,您无需执行任何操作,因为它会自动发生。
  3. 这有点无关紧要,但是您不需要自己停止订阅。 事实证明,流星和铁路由器会在计算停止时(即,当您导航到新路线时)自动执行此操作。

我希望这有帮助!

尝试将PostShowController添加到相应的路由:

this.route('postShow', {
  path: '/posts/:_id',
  controller: 'PostShowController'
});

也许我缺少了一些东西,但是在您的控制器中您说

data: function () {
  return Posts.findOne({_id: this.params._id});
},

但是在您的模板中,您使用的是{{body}},但我看不到任何地方被分配了任何东西。 不需要那样做:

data: {
  body: function () {
    return Posts.findOne({_id: this.params._id});
  }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM