FlowRouter не определен в отдельных шаблонах

Используя 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 в моих шаблонах.

Что мне не хватает?


person MastaBaba    schedule 20.02.2021    source источник
comment
Вы импортируете FlowRouter в файл шаблона?   -  person Jankapunkt    schedule 21.02.2021
comment
Я нет, явно. Насколько я понял, при запуске router.js он загружается.   -  person MastaBaba    schedule 22.02.2021
comment
Я обновил свой вопрос информацией о router.js.   -  person MastaBaba    schedule 22.02.2021
comment
Это все еще не отвечает, делаете ли вы import { FlowRouter } from ... в своем файле шаблона   -  person Jankapunkt    schedule 22.02.2021
comment
Ах. Я понимаю. У меня сложилось впечатление, что включение FlowRouter в начале было эквивалентно его импорту на протяжении всего проекта.   -  person MastaBaba    schedule 22.02.2021
comment
Чтобы было понятнее, я импортирую FlowRouter в router.js.   -  person MastaBaba    schedule 22.02.2021
comment
Ты прав. Мне пришлось включить FlowRouter в user_group.js. :/ Если вы ответите на это, я могу принять.   -  person MastaBaba    schedule 22.02.2021


Ответы (1)


Вам нужно импортировать FlowRouter в каждый js, который его активно использует (в вашем примере шаблон):

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

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