简体   繁体   中英

Inject Ember service from subfolder

My current ember project is structured like this:

+-- app
| services 
|   +-- service1.js
|   |   subfolder
|   |   +-- service2.js

I have a service (service1.js) just in the root of the services folder. I can perfectly inject that into a controller like this:

    service: Ember.inject.service("service1"),

But I want some more structure in my services folder. I want to put a service (service2.js) in a subfolder ('subfolder').

How do I inject this into my controller.js/component.js? If I write down:

service: Ember.inject.service("subfolder/service2") or
service: Ember.inject.service("subfolder.service2"),

This doesn't work

Attempting to inject an unknown injection

How can I inject a service from a subfolder into my controller/component/etc ?

Your code should work. Here is the working twiddle .

services/subfolder/service2.js

import Ember from 'ember';
export default Ember.Service.extend({
  name:'kumkanillam'
});

Inject service in the controller myService:Ember.inject.service('subfolder/service2'), . You can use it inside application.hbs like {{myService.name}}

And in 2021 these days with ember.js Glimmer/Octane:

import { inject as service } from '@ember/service';
export default class UserPlannerBasketOnlyController extends Controller {
     @service('tkc/rangemappings-tkc') rangemappings;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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