【问题标题】:Redirect route in meteorjs在meteorjs中重定向路由
【发布时间】:2013-05-10 11:12:47
【问题描述】:

所以我想在meteor.js 中创建一个bit.ly 类型的站点。我不知道如何重定向页面。我使用了主干.js 的路线,这是有效的。理想情况下,它会从数据库中获取链接,创建链接并重定向到它。我试过 window.location 但这不能正常工作 js文件:

  if (Meteor.isClient) {
    var Router = Backbone.Router.extend({
      routes: {
        "" : "main",
        "/" : "main",
        "help" : "help",
        'help/' : "help",
      },
      main: function() {
        Session.set('currentPage', 'homePage');
      },
      help: function() {
        Session.set('currentPage', 'helpPage');
      }
    });
    var app = new Router;
    Meteor.startup(function () {
      Backbone.history.start({pushState: true});
    });
    Template.home.homePage = function(){
      return Session.get("currentPage") == 'homePage';
    };
    Template.help.helpPage = function(){
      return Session.get("currentPage") == 'helpPage';
      //I want to do a redirect here somewhere:
      //window.location = 'http://google.com';
    };
  }

html:

<head>
  <title>My app name</title>
</head>

<body>
 {{> home}}
 {{> help}}
</body>

<template name="home">
    {{#if homePage}}
      <h1>Home Page</h1>
    {{/if}}
</template>

<template name="help">
    {{#if helpPage}}
        <h1>Help Page</h1>
    {{/if}}
</template>

【问题讨论】:

    标签: javascript backbone.js meteor handlebars.js


    【解决方案1】:

    导航到外部网址。您可以覆盖路由器导航

    if (Meteor.isClient) {
     var Router = Backbone.Router.extend({
     routes: {
      "" : "main",
      "help" : "help",
      'about' : "about",
     },
     navigate: function (url) { window.location = url; },
     main: function() {
      Session.set('currentPage', 'homePage');
     },
     help: function() {
      Session.set('currentPage', 'helpPage');
     },
     about: function() {
      Session.set('currentPage', 'about');
     }
    });
    
    var app;
    Meteor.startup(function () {
     app = new Router;
     Backbone.history.start({pushState: true});
    });
    Template.content.homePage = function(){
     return Session.get("currentPage") == 'homePage';
    };
    Template.content.helpPage = function(){
     return Session.get("currentPage") == 'helpPage';
    };
    Template.content.aboutPage = function(){
     return Session.get("currentPage") == 'about';
    };
    
    //I will add a helper below that will redirect to google everytime about link is     clicked. Open console to see message logged before redirect
    
    Template.about.rendered = function(){
     console.log('About is about to be rendered but wait..... we redirect to google.com');
     app.navigate('http://google.com');
    };
    

    【讨论】:

    • 如果你不想覆盖导航,那么只需使用这个window.location.href = 'http://google.com';
    【解决方案2】:

    我已经修改了您的 html 模板以帮助解释如何使用 Backbone.history.navigate 进行重定向。我添加了两个名为 content 和 about 的附加模板以及 3 个指向 home、help 和 about 三个路由的静态链接。内容取决于我们的模板助手 homePage、helpPage 或 aboutPage 返回的内容

    <head>
     <title>My app name</title>
    </head>
    
    <body>
     <a href='/'>Home</a>
     <a href='/help'>Help</a>
     <a href='/about'>About</a>
     {{> content}}
    </body>
    
    <template name='content'>
    {{#if homePage}}
        {{> home}}
    {{/if}}
    
    {{#if helpPage}}
        {{> help}}
    {{/if}}
    
    {{#if aboutPage}}
        {{> about}}
    {{/if}}
    </template>
    
    <template name="home">
      <h1>Home Page</h1>
    </template>
    
    <template name="help">
        <h1>Help Page</h1>
    </template>
    
    <template name="about">
        <h1>About Page</h1>
    </template>
    

    JS 现在是

    if (Meteor.isClient) {
    var Router = Backbone.Router.extend({
      routes: {
        "" : "main",
        "help" : "help",
        'about' : "about",
      },
      main: function() {
        Session.set('currentPage', 'homePage');
      },
      help: function() {
        Session.set('currentPage', 'helpPage');
      },
      about: function() {
        Session.set('currentPage', 'about');
      }
    });
    
    Meteor.startup(function () {
      var app = new Router;
      Backbone.history.start({pushState: true});
    });
    Template.content.homePage = function(){
      return Session.get("currentPage") == 'homePage';
    };
    Template.content.helpPage = function(){
      return Session.get("currentPage") == 'helpPage';
     };
    Template.content.aboutPage = function(){
      return Session.get("currentPage") == 'about';
    };
    
    //I will add a helper below that will redirect to homePage everytime about link is clicked. Open console to see message logged before redirect
    
    Template.about.rendered = function(){
      console.log('About is about to be rendered but wait..... we redirect to home');
      Backbone.history.navigate( "/", true);
    };
    }
    

    【讨论】:

    • 这可行,但当我尝试将其引导到异地时却不行。 Backbone.history.navigate( "http://google.com", true); 转至:localhost:3000/http://google.com。您将如何导航到其他域?
    • 我要添加另一个答案
    【解决方案3】:

    使用

    Backbone.history.navigate('/help', trueorfalse);
    

    如果要运行更新会话变量的路由器方法,则为 true

    【讨论】:

    • 你能扩展一下吗,我对流星/主干还很陌生。也许是一个重定向示例?
    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 2021-09-09
    • 2011-08-19
    • 2017-04-12
    • 2020-05-09
    • 2021-07-22
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多