0

当我在模板的帮助程序中返回非静态的东西时,Meteor 会抛出错误“Deps 重新计算异常:用户名”。

Router.configure({
  layoutTemplate: 'layout'
});

Router.map(function() {
  this.route('home', {path: '/'});
  this.route('dashboard');
})

if (Meteor.isClient) {
  Accounts.ui.config({
    passwordSignupFields: 'USERNAME_AND_EMAIL'
  });

  Template.dashboard.helpers({
    username: function() {
      return Meteor.user().username;
    }
  });
}


<template  name="layout">
  <h1>Layout</h1>
  {{yield}}
</template>

<template name="home">
  {{#link route='dashboard'}}Dashboard{{/link}}
</template>

<template name="dashboard">
  {{#link route='home'}}Home{{/link}}
  {{username}}
</template>

奇怪的是,当通过单击主页模板中的链接访问仪表板路线时,一切正常。但是当我在 url 栏中输入 /dashboard 时,我得到了那个错误。

4

1 回答 1

1

当您手动导航到 URL 时,它会强制流星重新运行登录过程。因此,虽然它这样做,但Meteor.user()不会返回有效对象(因此您无法访问username)。你有两个选择:

为您的代码添加一个守卫,例如:

Meteor.user() && Meteor.user().username;

或让您的路由器在登录过程中显示“登录”页面。

于 2014-01-16T21:36:16.680 回答