简体   繁体   中英

How declare and import variable into component on angular 2 webpack?

I try to declare variable into some *.ts file and import file into component .ts file to use.

url.ts :

var loginComponentTemplateUrl: string;
if (true) {
    loginComponentTemplateUrl = './login.component.html';
} else {
    loginComponentTemplateUrl = './app/login/login.component.html';
}

login.component.ts :

import './url';
@Component({
    templateUrl: loginComponentTemplateUrl
})
...

In SystemJS all working, but when I try to use Webpack build an error: loginComponentTemplateUrl variable not defined . I need to import this url.ts file into polyfills.ts or vendor.ts but how? as I understand vendor.ts/polyfills.ts files working only with modules .

You have to export the loginComponentTemplateUrl variable from url.ts and import it in login.component.ts

something like this in url.ts

let loginComponentTemplateUrl: string;
if (true) { //will throw linting error
   loginComponentTemplateUrl = './login.component.html';
} else {
    loginComponentTemplateUrl = './app/login/login.component.html';
}

export default loginComponentTemplateUrl

And, in login.component.ts

import loginComponentTemplateUrl from './url'
@Component({
     templateUrl: loginComponentTemplateUrl
})
...

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