2016-09-09 42 views
0

我有一個需要存儲和路由器的餘燼組件。我正在嘗試使用Ember.inject.service注入它們。它適用於當我注入商店,但不是路由器:Ember依賴注入不能在Ember中使用路由器1.11

import Ember from 'ember'; 

const MyComponent = Ember.Component.extend({ 
    store: Ember.inject.service('store'), 
    routing: Ember.inject.service('-routing'), 

    classNames: ['my-component'], 
    schema: Ember.computed('store', 'routing', function schema() { 

    // This works. Shows up when I log it. 
    const store = this.get('store'); 

    // This doesn't 
    const routing = this.get('routing'); 

    // return schema from store. This works. 
    }), 
}); 

export default MyComponent 

我在Ember版本1.11.3。我錯過了什麼嗎?

+0

您是否檢查過包裝中是否含有此物質? 'ember-routing/lib/services/routing.js'也許你只是想念這個文件。另外,你有什麼錯誤嗎?例如,你是否嘗試從初始化程序注入? – reillyethan

+0

@reillyethan:什麼包?在燼中構建自己?沒有錯誤。目前我正在注入組件初始化。 – BarFooBar

+0

我以爲也許路由服務安裝不正確或不正確,但沒有任何意義。那麼,我不知道爲什麼它不起作用。 Docs表示這項服務是私密的,但可以打開。我不知道這是什麼意思,你可以看到[這裏]的評論(https://github.com/emberjs/ember.js/blob/v2.7.0/packages/ember-routing/lib/services/ routing.js#L13)也許它不是像商店那樣開放或者不適合 – reillyethan

回答

0

我不知道灰燼1.13,但在我的灰燼2.x的應用程序,我注入了路由器,你如上所述,然後可以轉換如下:

this.get('routing.router').transitionTo('routeName', model, { 
     queryParams: { 
     queryName: queryValue 
     } 
    }); 

PS:人們普遍認爲最佳實踐是將操作閉包傳遞給將處理路由轉換的組件,而不是使組件負責處理轉換。