gpt4 book ai didi

javascript - meteor 铁路由器动态段不工作

转载 作者:行者123 更新时间:2023-11-30 05:41:45 26 4
gpt4 key购买 nike

我一直在研究 meteor ,它太棒了!我一直在寻找路由解决方案,我发现很酷的“iron-router”,我设法让获取静态页面与模板一起工作,但是当我转到/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');
}
});
}

博客.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. 从 Controller 中删除 run 方法。该方法实际上做了很多事情,因此如果您想覆盖渲染,您可以改用 action 方法。但在您的情况下,您不需要执行任何操作,因为它会自动发生。
  3. 这有点无关,但您不需要自己停止订阅。事实证明,Meteor 和 iron-router 会在计算停止时(即当您导航到新路线时)自动执行此操作。

希望对您有所帮助!

关于javascript - meteor 铁路由器动态段不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20433645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com