流星:重定向到另一个网址

时间:2016-06-07 11:10:38

标签: redirect meteor

是否可以在Meteor框架中从服务器端向客户端发送重定向。

我找不到这个功能。

是吗,请你举个例子。

1 个答案:

答案 0 :(得分:1)

您的客户端和服务器端之间有两种通信方式:

  • 集合
  • Meteor.methods

我们假设你有:
  - FlowRouter
  - Blaze
  - FlowRouter Blaze
  - 自动发布

使用馆藏

您可以执行以下操作:
集合文件/collections/Events.js

// Create a global collection 
Events = new Meteor.Collection("Events");

服务器端的某个地方:

Events.insert({
    type: 'redirect',
    route: 'home'
    // require user_id if you want this to work with multi users
    params: {}
});

客户端路线:

FlowRouter.router('/some/url', {
     action: function () {
          BlazeLayout.render('Layout', {main: 'displayedTemplate'});
     },
     name: 'home'
});

客户端布局:

Template.Layout.onCreated(function () {
    // suscriptions to Events if autopublish have been removed...?
    this.autorun(function() {
        const event = Events.findOne();
        switch (event.type) {
            case 'redirect':
               FlowRouter.go(event.route, event.param);
               Events.delete(event);
            ...
        }
    });
});

使用方法

当用户做某事并且您想要重定向他时:

 Template.Layout.events({
    'click h2': function (event, template) {
        Meteor.call('getCoffee', function (err, event) {
            if (err) {
               //handle error
            } else {
                if (event.type === 'redirect') {
                     FlowRouter.go(event.route, event.param);
                }
            }
        });
    }
})
相关问题