如何在angular 2 webpack上声明变量并将其导入组件? [英] How declare and import variable into component on angular 2 webpack?
问题描述
我尝试将变量声明到某些 *.ts
文件中,然后将其导入到组件 .ts
文件中以供使用.
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
})
...
在 SystemJS
中都可以正常工作,但是当我尝试使用 Webpack
时,会生成错误: loginComponentTemplateUrl变量未定义
.我需要将此 url.ts
文件导入到 polyfills.ts
或 vendor.ts
中,但是如何?据我了解, vendor.ts/polyfills.ts
文件仅适用于 modules
.
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
.
推荐答案
您必须从 url.ts
中 export
变量 loginComponentTemplateUrl
和在 login.component.ts
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
然后,在 login.component.ts
import loginComponentTemplateUrl from './url'
@Component({
templateUrl: loginComponentTemplateUrl
})
...
这篇关于如何在angular 2 webpack上声明变量并将其导入组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!