userqwert userqwert - 1 year ago 119
AngularJS Question

import javascript variable as constant angular

I'm trying to import a JS variable as a constant into my application, as I am unable to use conventional JSON methods.

I have this code:

var english = {"title-1": "Some text"};
export { english as default };

And am importing it like this:

import { english } from '../path/english.js';

angular.module('myApp', ['ui.router'])

.constant('english', english)

And then trying to use it like this:

export class AController {
constructor($scope, english) {
'ngInject'; = english; //undefined

What am I doing wrong here? english is undefined.

Answer Source

If you want english to be the default export, you need the default keyword and should have the following structure:

var english = {"title-1": "Some text"};
export default english;

and you would import it without the {} like so:

import english from '../path/english';

To have english simply be one of exported items you would leave off the default keyword.

export var english = {"title-1": "Some text"};

and to import it, the {} are required.

import {english} from '../path/english';

So in your case, either remove the default keyword when you export it, or remove the {} when you import it.

import and export documentation from mdn.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download