1

I have a Configuration JS file where some config variables are there. For ex :

var config = {url:"xyz.com"};

I want to access this config parameters through out my application.

As export is the right way to export anything i Tried export var appConfig = config

but its giving error saying can not get property url of undefined.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

3 Answers3

4
  1. First you will have to allowJs to true in the tsconfig.json compiler options:

{ "compilerOptions": { "allowJs": true,

  1. And then,

create a javascript file within the app folder. You can create the javascript file in any folder or any component. For example app/javascriptTest.js

  1. In app/javascriptTest.js for example you want to export a variable, A, In the blank app/javascriptTest.js file you type:

export var A = 2;

  1. You go to the typescript file you want to import the variable A to, for example in app/componentName/header.ts you import at the top with:

import { A } from "../javascriptTest.js"

  1. Finally within app/componentName/header.ts typescript file you can use the A variable as it is.

let x="Hello "+A;

I hope that answers your question.

Kyrul
  • 174
  • 6
0

You can provide it using Angulars DI using a string key (or OpaqueToken) and request it by @Inject('keyName'):

var config = {url:"xyz.com"};

bootstrap(AppComponent, [provide('myConfig', {useValue: config})]);
export class SomeComponent { // or SomeService
  constructor(@Inject('myConfig') private config) {}
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Actually the config variable is going to get used outside the class. For ex : @Component({ selector: 'app-page', templateUrl: "ui/"+envConfig.platform+".html" }) – Snehasis Mohapatra May 04 '16 at 11:28
  • Then you are probably looking for something like http://stackoverflow.com/questions/36434546/using-require-to-set-templateurl-in-angular-2-while-getting-error – Günter Zöchbauer May 04 '16 at 11:37
  • No, this is Fine, what my issue is the Path which needs to get required in the templateURI is dynamic which will come from the config variable for Ex : @Component({ selector: 'app-page', templateUrl: "ui/"+config.platform+".html" }) – Snehasis Mohapatra May 04 '16 at 11:40
  • I guess you can create property on `window` and load from there. Before bootstrap `window.myConfig = {url:"xyz.com"};` and then use it like `window.myConfig.url` – Günter Zöchbauer May 04 '16 at 11:50
  • Yes I am using it like this right now and It works fine, but the issue is it shows error or tslint so is not a valid code, there must be a way to do so. I tried below stuffs but not working. http://stackoverflow.com/questions/27417107/how-use-an-external-non-typescript-library-from-typescript-without-d-ts – Snehasis Mohapatra May 04 '16 at 11:51
  • tslint just doesn't know about `window.xxx` that doesn't mean it's invalid. You can make it known but I don't use TS/JS (only Dart) and don't know about these details. – Günter Zöchbauer May 04 '16 at 11:55
  • Ok, thanks for your time to write , I will keep trying some other ways – Snehasis Mohapatra May 04 '16 at 12:02
0

I want to access this config parameters through out my application.

Just export config.ts:

export const config = {url:"xyz.com"};

And then import as needed:

import {config} from "./path/to/config";

And yes. No .ts at the end.

basarat
  • 261,912
  • 58
  • 460
  • 511