【问题标题】:FlowRouter is not defined in individual templatesFlowRouter 未在单个模板中定义
【发布时间】:2021-05-23 06:07:33
【问题描述】:

使用 Meteor,我从一个默认的 Meteor 项目开始...

meteor create --full

我在 router.js 中添加了一个路由,如下所示:

FlowRouter.route('/group/:_id', {
  name: 'App.groups.show',
  action() {
    if (!logged_in()) {
      FlowRouter.go("App.home");
    }
    else {
      this.render('App_body', 'Group');
    }
  },
});

router.js 在这里:

/imports/startup/client/router.js

组模板是这样的:

<template name="Group">
  
  {{> user_group}}

</template>

对于 user_group,我有这个:

Template.user_group.onCreated(function user_groupOnCreated() {
  console.log("id", FlowRouter.getParam('_id'));
});

这会导致:

ReferenceError: FlowRouter is not defined
    at Blaze.TemplateInstance.user_groupOnCreated (user-group.js:46)
    at template.js:119
    at Function.Template._withTemplateInstanceFunc (template.js:490)
    at fireCallbacks (template.js:115)
    at Blaze.View.<anonymous> (template.js:195)
    at fireCallbacks (view.js:276)
    at Object.Tracker.nonreactive (tracker.js:603)
    at view.js:273
    at Object.Blaze._withCurrentView (view.js:533)
    at Object.Blaze._fireCallbacks (view.js:272)

我也无权访问我的模板中的FlowRouter.go。

我错过了什么?

【问题讨论】:

  • 你在模板文件中导入FlowRouter吗?
  • 我没有,明确的。据我了解,通过将 router.js 置于启动状态,它会被加载。
  • 我用 router.js 上的信息更新了我的问题。
  • 这仍然无法回答,无论您是否在模板文件中使用import { FlowRouter } from ...
  • 啊。我知道了。我的印象是,一开始就包含 FlowRouter 相当于在整个项目中导入它。

标签: javascript meteor meteor-blaze


【解决方案1】:

您需要在每个积极使用它的 js 中导入FlowRouter(在您的示例中为模板):

import { FlowRouter } from 'meteor/kadira:flow-router'

Template.user_group.onCreated(function user_groupOnCreated() {
  console.log("id", FlowRouter.getParam('_id'))
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多