简体   繁体   中英

EmberJs: Not able to resolve config/environment.js from a service

Ember version - ~2.8.0

This might seem really simple, but it has got on to nerves after hours of looking through for a fault.

I am trying to simply import the config/environment.js file into a service(at app/services/myservice.js) and make use of the contents. like this -

import config from "../config/environment";

But, all i get is "Cannot read property 'APP' of undefined" in the console, if i do config.APP . Even though environment.js is present physically, under demo-app/config/environment.js .

i am confused, if i am missing some convention, or some setting somewhere that needs to be present, in order for this to resolve correctly.

Please help.

Also(Although this was obvious), if i change the import as import config from "../../config/environment"; , it says Could not find module 'config/environment' imported from 'demo-app/services/myservice'

I do understand the app folder is skipped in the resolution. ie the physical address of demo-app/app/something... becomes demo-app/something... . But some one please explain why it may not be resolving. It is in the fresh installation of the App.

import ENV from 'demo-app/config/environment'; works for me ( demo-app is a package name and usually it's the same as your project root directory name). A big advantage of this method is that you don't need to think how much ../ you need to put, just drop the same line in any of your files.

I prefer using resolveRegistration of ApplicationInstance such as:

let conf = Ember.getOwner(this).resolveRegistration('config:environment');

Pass your service as this .

在您的情况下,这将是正确的相对路径,

import config from "./../config/environment";

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