ember.js 如何在适配器emberjs中注入服务?

8oomwypt  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(160)

我尝试在我的适配器中注入服务,但收到此错误:“Assert失败:尝试在没有容器的对象上查找插入的属性,请确保该对象已通过容器示例化。”
有人能告诉我如何在适配器中注入服务吗?

//services/api.js

import AjaxService from 'ember-ajax/services/ajax';
import ENV from 'nfe-customer-console-v2/config/environment';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';

export default AjaxService.extend({
    session: service(),
    host: ENV.APP.API_URL,

    headers: computed('session.authToken', {
        get() {
            let headers = {};
            const authToken = this.get('session.data.authenticated.access_token');
            if (authToken) {
                headers['Authorization'] = `Bearer ${authToken}`;
            }
            return headers;
        }
    })
});

//adapters/conta.js

import EmberObject from '@ember/object';
import {inject as service} from '@ember/service';

export default EmberObject.extend({
  api: service(),

  changePassword(model) {
    return this.get('api').request('/users/change_password', {
      method: 'POST',
      data: {
            oldPassword: model.oldPassword,
            newPassword: model.newPassword,
            confirmPassword: model.confirmPassword
          },
    })
  }
})

Error: "Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container."
7d7tgy0s

7d7tgy0s1#

您可以使用初始设定式来插入工作阶段。例如,使用'ember-simple-auth':

// app/initializers/session.js
export function initialize(application) {
  application.inject('route', 'session', 'service:session');
  application.inject('controller', 'session', 'service:session');
  application.inject('adapter', 'session', 'service:session');
}

export default {
  name: 'session',
  after: 'ember-simple-auth',
  initialize
};

相关问题