Ember 2.16在路由/应用程序中创建状态哈希时出错

时间:2017-11-21 15:30:56

标签: ember.js ember-data ember-cli

我刚刚接受反应的余烬。在routes / application.js里面我想使用" state" hash用于保存支持函数,model()和actions()之外的变量。

这是有效的:

  session: Ember.inject.service(),
  servers: null,
  clients: null,
  variable3: null,
  variable4: null,
  variable3_id: null,
  selectedDate: null,
  currentUser: Ember.inject.service(),

这就是我想要做的事情:

state: {
    session: Ember.inject.service(),
    servers: null,
    clients: null,
    variable3: null,
    variable4: null,
    variable3_id: null,
    selectedDate: null,
    currentUser: Ember.inject.service(),
},

当我访问它时,比如get,get(this, 'state.session')我收到此错误:

ember.debug.js:29034 Error while processing route: analytics Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container. Error: Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.

当我不将变量放在我的state哈希中,并使用get,get(this, 'session')访问时,我不会遇到任何问题。这里发生了什么?我提供了足够的信息还是我错过了什么?

由于

1 个答案:

答案 0 :(得分:1)

欢迎来到ember.js。

如错误消息中所述,state不是由ember容器创建的,因此ember无法向其注入服务。控制器,服务,路由,组件,实例初始化器由ember创建,但不是普通对象。

使用组件或控制器来保存state。您不需要使用单独的状态对象。 component属性实际上就是它的状态。

另一方面,任何service(主要是单身人士)都不应该是某种状态。

最后,当然有一些方法可以将service添加到state个对象中。但这不是常见用法。

相关问题