Ember 2.16 在 routes/application 中创建状态哈希时出错

Ember 2.16 Error when creating a state hash in routes/application

我是 ember 的新手,来自 React。在 routes/application.js 中,我想使用 "state" 散列来保存支持函数、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') 我没有遇到任何问题。这里发生了什么?我提供了足够的信息还是遗漏了什么?

谢谢

欢迎来到ember.js。

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

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

另一方面,任何 service(主要是单例)不应该是某物的状态。

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