meteor 基本订阅和发布
示例
首先,删除autopublish。autopublish自动将整个数据库发布到客户端,因此无法看到发布和订阅的效果。
删除autopublish:
$ meteor remove autopublish
然后,您可以创建出版物。下面是一个完整的示例。
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
const Todos = new Mongo.Collection('todos');
const TODOS = [
{ title: 'Create documentation' },
{ title: 'Submit to Stack Overflow' }
];
if (Meteor.isServer) {
Meteor.startup(function () {
TODOS.forEach(todo => {
Todos.upsert(
{ title:todo.title},
{ $setOnInsert: todo }
);
});
});
//第一个参数是名称。
Meteor.publish('todos', function () {
return Todos.find();
});
}
if (Meteor.isClient) {
//通过名称订阅出版物。
Meteor.startup(function () {
Meteor.subscribe('todos');
})
}
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短