首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:尝试注入未知注入:'service:i18n‘

错误:尝试注入未知注入:'service:i18n‘
EN

Stack Overflow用户
提问于 2017-02-08 23:45:08
回答 1查看 2.5K关注 0票数 1

我对ember-i18n有以下问题:

代码语言:javascript
复制
Error: Attempting to inject an unknown injection: 'service:i18n'
at Registry.validateInjections (ember.debug.js:2303)
at ember.debug.js:1538
at runInDebug (ember.debug.js:5813)
at Object.runInDebug (ember.debug.js:17396)
at instantiate (ember.debug.js:1532)
at lookup (ember.debug.js:1385)
at Container.lookup (ember.debug.js:1304)
at Class.lookup (ember.debug.js:32564)
at Class._internalGetHandler (router-ext.js:107)
at Class._getHandlerForEngine (router-ext.js:84)

我在ember引擎中使用ember-i18n插件。我的i18n初始化器addon/instance-initializers/i18n.js

代码语言:javascript
复制
export default {
name: 'i18n',

initialize: function(applicationInstance) {
    const i18nService = applicationInstance.lookup('service:i18n');
    i18nService.set('defaultLocale', window.I18n.defaultLocale);
    i18nService.set('locale', window.I18n.locale);
}
};

在我的索引路由(routes/index.js)中,我有以下代码:

代码语言:javascript
复制
import Ember from 'ember';

export default Ember.Route.extend({
i18n: Ember.inject.service(),

actions: {
    didTransition() {
        Ember.run.scheduleOnce('afterRender', this, function() {
            console.log(this.get('i18n').t('error_empty_user_data'));
            // ....
        });
    }
}
});

如何声明i18n服务?

我使用ember-18n 4.5.0版本和2.10.0 ember-cli版本。

EN

回答 1

Stack Overflow用户

发布于 2017-02-08 23:56:58

初始化器应该在i18n one之后运行。

代码语言:javascript
复制
export default {
  name: 'i18n',
  after: ['ember-i18n'],

  initialize: function(applicationInstance) {
    const i18nService = applicationInstance.lookup('service:i18n');
    i18nService.set('defaultLocale', window.I18n.defaultLocale);
    i18nService.set('locale', window.I18n.locale);
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42117557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档