简体   繁体   English

Ember.js-如何在其他服务中使用服务?

[英]Ember.js - How can I use a service in another service?

I have setup two services as shown in the below initializer: 我已经设置了两个服务,如下面的初始化程序所示:

/* Service Initaializers */
var Initaializer = {
    name: 'Services',
    initialize: function(Container, App) {
        /* Inject Session Service In To All Routes And Controllers */
        App.inject('route', 'Session', 'service:session');
        App.inject('controller', 'Session', 'service:session');
        /* Inject Debug Service In To All Routes And Controllers */
        App.inject('route', 'Debug', 'service:debug');
        App.inject('controller', 'Debug', 'service:debug');
    }
};

/* Export */
export default Initaializer;

I can access both the session service and debug session from my routes/controllers user this.Session and this.Debug . 我可以从我的路由/控制器用户this.Sessionthis.Debug访问会话服务和调试会话。

What I am having trouble doing is accessing any of the functions in the Debug service from the session service. 我遇到的麻烦是从会话服务访问调试服务中的任何功能。

app/services/debug.js 应用程序/服务/ debug.js

/* Debug Service */
var Service = Ember.Object.extend({
    /* Start Debug */
    init: function() {
        console.log('Debug Started!'); // This does appear in the console.  
    },  
    logSomething: function(i) {
        console.log(i);  // This does work from all routes/controllers. 
    }
});

/* Export */
export default Service;

app/services/sessions.js 应用程序/服务/ sessions.js

/* Session Service */
var Service = Ember.Object.extend({
    /* Start Session */
    init: function() {  
        console.log('Session Started!'); // This does appear in the console.
        this.Debug.logSomething('Test'); // This gives an error.
    },
    sayHi: function() {
        console.log('Hello From The Session Service'); // I does work from all routes/controllers.
    }
});

/* Export */
export default Service;

The line that gives a console error is this.Debug.logSomething('Test'); 给出控制台错误的行是this.Debug.logSomething('Test'); .

The error is: Uncaught TypeError: Cannot read property 'logSomething' of undefined 错误是: Uncaught TypeError: Cannot read property 'logSomething' of undefined

What do I need to do in order to access a function in another service FROM a service? 为了从服务访问另一个服务中的功能,我需要做什么?

You have only injected these objects in to the route and controllers. 您仅将这些对象注入到路由和控制器中。 You actually need to inject in to each other if you want them accessible 如果您希望他们之间互通有无,您实际上需要相互注入

Ok, so I believe this is possible. 好的,所以我相信这是可能的。 You just need to inject your Debug object in to your session one. 您只需要将Debug对象插入到会话1中即可。

You can do this like so: 您可以这样做:

First register your factories: 首先注册您的工厂:

App.register('utils:debug', App.Debug);
App.register('service:session', App.Session);

And then inject the debug in to the session: 然后将调试插入到会话中:

App.inject('service:session', 'debug', 'utils:debug');

Or you can inject debug in to all services: 或者,您可以将调试插入到所有服务中:

App.inject('service', 'debug', 'utils:debug');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM