Meteor 我的路线怎么了
我正试图将路由更改为,但将router.js中的参数从“_id”更改为“title”时,我得到的是空页面 我有一个表格可以很好地工作:Meteor 我的路线怎么了,meteor,iron-router,Meteor,Iron Router,我正试图将路由更改为,但将router.js中的参数从“_id”更改为“title”时,我得到的是空页面 我有一个表格可以很好地工作: Template.addpost.events({ 'submit form': function(e) { e.preventDefault(); var query = { title: $(e.target).find('[name=title]').val(), te
Template.addpost.events({
'submit form': function(e) {
e.preventDefault();
var query = {
title: $(e.target).find('[name=title]').val(),
text: $(e.target).find('[name=text]').val() ,
image: $(e.target).find('[name=image]').val(),
intro: $(e.target).find('[name=intro]').val(),
friendlyTitle: slugify($(e.target).find('[name=title]').val()),
author: Meteor.user().profile.name
};
query._id = Posts.insert(query);
Router.go('index');
}
});
和my router.js:
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
waitOn: function() { return Meteor.subscribe('posts'); }
});
Router.map(function() {
this.route('index', {path: '/'});
this.route('addpost', {path: '/add'});
this.route('postPage', {
path: '/posts/:friendlyTitle', //empty page with friendlyTitle, but with _id working good
data: function() { console.log(this.params.friendlyTitle);return Posts.findOne(this.params.friendlyTitle); //same, with _id working good}
});
});
Router.onBeforeAction('loading');
postPage.html:
<template name="postPage">
<div class="container main">
<div class="col-md-12"><h1>{{title}}</h1></div>
<div class="col-md-12">
{{{text}}}
</div>
</div>
</template>
{{title}}
{{{text}}}
您需要指定查询条件
你有这个
return Posts.findOne(this.params.friendlyTitle);
换成这个
return Posts.findOne({title:this.params.friendlyTitle});
Router.route('/posts/title', {
name: 'postPage',
waitOn:function(){
return Meteor.subscribe('posts'); //or use the new subscritionReady
},
data: function() {
console.log(this.params.title)
console.log(Posts.findOne({title:this.params.title});)
return Posts.findOne({title:this.params.title});
}
});
如果没有,则查找将采用this.params.friendlyTitle
作为\u id
,并返回空查询
路线更干净*
把路线改成这个
return Posts.findOne({title:this.params.friendlyTitle});
Router.route('/posts/title', {
name: 'postPage',
waitOn:function(){
return Meteor.subscribe('posts'); //or use the new subscritionReady
},
data: function() {
console.log(this.params.title)
console.log(Posts.findOne({title:this.params.title});)
return Posts.findOne({title:this.params.title});
}
});
使用Router.map
Router.map(function () {
this.route('postPage', {
path: '/posts/:title',
waitOn:function(){
return Meteor.subscribe('posts'); //or use the new subscritionReady
},
data: function(){
return Posts.findOne({title:this.params.title});
}
});
});
对每条路线使用différentes
Router.map
或Router.route
。要清楚,您需要为帖子/标题和帖子/:id创建单独的路线,对吗?我想bartezr不再需要帖子/:\u id
路线了