我试图在适配器中注入服务,但收到此错误:“断言失败:尝试在没有容器的对象上查找注入的属性,请确保该对象是通过容器实例化的。”
有人可以告诉我如何在适配器中注入服务吗?
//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."
答案 0 :(得分:0)
您可以使用初始化程序来注入会话。 例如,使用'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
};