我在 Angular 2 应用程序中导入 html 模板时遇到问题。
在 globals.d.ts
declare module "*.html" {
const content: string;
export default content;
}
在 app.component.ts
import { Component } from '@angular/core';
import './app.component.css';
import * as template from './app.component.html';
@Component({
selector: 'app',
template: <string>template
})
export class AppComponent {
constructor() {
console.log('I am App');
}
}
该应用程序正常工作并且模板已加载,但我收到此错误 TS2352:类型 'typeof "*.html"' 无法转换为类型 'string'。有人有解决方法吗?
我想改为这样做。
import template from './app.component.html';
@Component({
template: template
})
...
但是转译的代码最终是这样的。
var app_component_html_1 = __webpack_require__(653);
...
template: app_component_html_1.default
app_component_html_1 是我需要的字符串。他们为什么要添加 .default?