【问题标题】:Meteor: set the starting page (location)Meteor:设置起始页(位置)
【发布时间】:2014-05-23 22:39:38
【问题描述】:

如何让 Meteor 应用在​​特定页面上启动?如果您不对此进行控制,则浏览器会记住您上次关闭应用程序时您在应用程序中的位置,并尝试加载该旧位置。这很好,如果这是你想要的,但如果不是,那你怎么办?

在我的应用程序中使用 Iron-router,这曾经可以工作:

Meteor.startup(function () {

  Router.go('/');

});

现在这会在 Router.js 中引发错误,抱怨 self._location 未定义。就好像对 Router.go() 的调用现在发生得太快了。有没有办法知道路由器何时准备好进行 go() 调用?或者在这里引入延迟的方法?

_________________EDIT_________________

为了解决这个问题,我已将所有内容简化为基本要素。这是我所有的代码,总共 2 个文件:

newPL.js:

Router.configure({

  layoutTemplate: 'layout'

});



Router.map(function () {

   this.route('home', {

     path: '/',

  });

  this.route('main', {

    path: '/main',

  });

});

if (Meteor.isClient) {
    Meteor.startup(function () {

     Router.go('home');

    });
}

和newPL.html:

<head>
  <title>newPL</title>
</head>

<template name='layout'>

    <div>
      {{> yield}}
    </div>

</template>

<template name='home'>

    <div>

      Welcome to Planet Lingo!

    <a href='/main'>GO</a>

    </div>

</template>

<template name='main'>

    <div>

    <h1>Main</h1>

    </div>

</template>

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    这看起来像是 Iron-router 中的一个错误。 Iron 路由器在 router.start 周围做了一个 Meteor.defer 参见:Iron-router source line 34

    你可以自己使用 Meteor.defer 来解决这个问题

    Meteor.startup(function () {
        Meteor.defer(function () {Router.go('home');});
    });
    

    这很好用,因为你的 defer 低于 Iron-router 创建的那个

    【讨论】:

    • 谢谢,但这给了我完全相同的结果。这就是我通常使用 Router.go() 的方式,但我在上面对其进行了更改以简化示例。 (Router.go() 实际上确实可以使用诸如“/”或“/home”之类的文字路径。)当它工作时,就是这样。对我来说,一直都是这样,除了在这个关键的启动过程中。
    • @MichaelMcC 我在我的一个应用程序上进行了尝试,但没有出现错误。你能分享一些你的路由器配置代码吗?
    • 编辑了问题以包含我的所有代码,这现在引发了一个更根本的错误 (????)。
    • 修复了新的错误——现在我又回到了旧的错误(无法读取未定义的属性集,self._location is router.js is undefined)
    • @MichaelMcC 找到了问题的原因并更新了我的答案
    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 2020-01-26
    • 2013-01-20
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多