我正在尝试构建一个多聊天室应用。我正在使用路由器,所以当用户点击我的网站上的链接(到聊天室,在代码中标记)时,它会将它们路由到新模板。但是当有人点击链接时,链接URL会显示在地址栏中,但它不会呈现页面。但是,如果我直接访问该链接,它将被渲染。所以基本上我的链接不会像他们应该的那样去。此外,当我点击链接时,我在控制台中收到错误:'来自Deps recexute的异常:'(下面的完整错误代码)
这是代码,首先是html,然后是Meteor javascript。我在html和js
中评论了代码的相关部分HTML:
<head>
<title>testChat</title>
</head>
<body>
<div class"row">
{{> room}}
{{renderPage}}
</div>
</body>
<template name="home">
<div class="span6">
<h1>Welcome to Meteor Chat</h1>
<p>Please a select a room to chat in, or create a new one</p>
</div>
</template>
<template name="room">
<table>
{{#each rooms}}
<tr>
<td><a href="/rooms/{{_id}}">{{name}}</a></td> <!-- THIS IS THE LINK! -->
<td>{{members}}</td>
<td>{{last_activity}}</td>
</tr>
{{/each}}
<tr>
<td colspan="3">
<input type="text" name="name" placeholder="Enter your chatroom name"></input>
<button type="submit" class="btn btn-primary pull-right">
<i class="icon-plus"></i> Create Room
</button>
</td>
</tr>
</table>
</template>
<template name="chatRoom">
<h1> You are in chat room {{id}} </h1>
</template>
Meteor Javascript:
Rooms = new Meteor.Collection('rooms');
if (Meteor.isServer && Rooms.find().count() == 0) {
var roomsA = [
{name: 'Meteor Talk', members: 3, last_activity: '1 minute ago', id: 0,
messages: [
{author: 'Tom', text: 'Hi there Sacha!'},
{author: 'Sacha', text: 'Hey Tom, how are you?'},
{author: 'Tom', text: 'Good thanks!'},
]},
{name: 'Meteor Development', members: 2, last_activity: '5 minutes ago', id: 1},
{name: 'Meteor Core', members: 0, last_activity: '3 days ago', id: 2}
];
for (var i = 0; i < roomsA.length; i++) {
Rooms.insert(roomsA[i]);
};
}
if (Meteor.isClient) {
Meteor.Router.add({
'/': 'home',
'add': function () { return 'test'},
'/rooms/:id': function(id) { //THIS IS THE ROUTER CALL TO THE ROOMS LINKS
Session.set('currentRoomId', id);
return 'chatRoom'
}
});
Template.room.rooms = function () {
return Rooms.find();
};
Template.chatRoom.id = function() {
var id = Session.get('currentRoomId', id);
return id;
};
console.log(Rooms.find().fetch());
}
如何点击我的链接不会呈现所请求的页面,但是当我直接访问该链接时,它会被渲染?
这是我点击链接时显示的完整控制台错误:
Exception from Deps recompute: _copyAttributes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:494
match@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:249
@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:61
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:17
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
_patch@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:79
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:639
operate@http://localhost:3000/packages/liverange/liverange.js?5e6bc5ba11645802c3440658c75fcc2277537dd1:458
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:640
withEventGuard@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:103
renderToRange@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:641
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:864
_compute@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:129
_recompute@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:142
flush@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:224
[native code]
答案 0 :(得分:1)
我认为,你的代码中只有拼写错误而不是
<div class+"row"> you wanted <div class="row">
..如果你纠正这个错字,你的申请将会有效。
也许有一点我认为这是使用事件处理而不是生成链接的更好方法。
HTML https://gist.github.com/rudovjan/6199753
<td><a class="goToRoom" href="#">{{name}}</a></td>
javascript就在这里 https://gist.github.com/rudovjan/6199750
Template.room.events({
'click .goToRoom': function(e, t){
console.log("You clicked");
console.log(this);
Meteor.Router.to('/rooms/' + this.id);
}
})