먼저 Ember CLI없이 작은 Ember 앱을 만들었습니다.
이 코드가 있습니다.
window.MyApp = Ember.Application.create({
ready: function() {
this.register('session:current', MyApp.SessionController, { singleton: true });
this.inject('controller', 'session', 'session:current');
}
});
이것은 효과가 있었다.
그런 다음 Ember CLI로 모든 것을 처음부터 다시 작성하기로 결정했습니다.
이전 버전과 마찬가지로 파일을 편집하고 후크를 app/app.js
추가했습니다 ready
.
var App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver,
ready: function() {
this.register('session:current', App.SessionController, { singleton: true });
this.inject('controller', 'session', 'session:current');
}
});
이것은 작동하지 않습니다.
세션 컨트롤러가 존재합니다. 이것이 파일의 내용입니다.app/controllers/session.js
export default Ember.Controller.extend({
isLoggedIn: false,
});
내가 얻는 오류 메시지는
TypeError: Attempting to register an unknown factory: `session:current`
브라우저에 나타납니다.
그 메시지를 검색했지만 Ember CLI에서 종속성 주입에 대해 아무것도 찾지 못했습니다.
어떤 생각?
ember-cli 에서이를 달성하기 위해 스텁을 사용 ember generate service <name of service>
하고 ember generate initializer <name of initializer>
빌드 할 수 있습니다 . 이는 app.js
.
기본적으로 다음과 같은 서비스를 만듭니다.
// app/services/notifications.js
import Ember from 'ember';
export default Ember.Object.extend({
initNotifications: function() {
// setup comes here
}.on('init'),
// Implementation snipped, not relevant to the answer.
});
그리고 서비스를 필요로하는 애플리케이션의 컴포넌트에 서비스를 주입하는 이니셜 라이저 :
// app/initializers/notifications-service.js
import Notifications from '../services/notifications';
export default {
name: 'notification-service',
after: 'auth-service',
initialize: function( container, app ) {
app.register( 'notifications:main', Notifications, { singleton: true } );
app.inject( 'component:system-notifications', 'notificationService', 'service:notifications' );
app.inject( 'service:auth', 'notificationService', 'service:notifications' );
}
};
이를 통해 notificationService
지정된 구성 요소에서 사용할 수 있습니다 .
Ember의 종속성 주입 주제에 대한 문서는 http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/ 에서 찾을 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다