Typescript 2通配符模块 [英] Typescript 2 wildcard module
问题描述
我无法在我的angular 2应用程序中导入html模板.
I am having trouble with importing html templates in my angular 2 app.
在globals.d.ts
In globals.d.ts
declare module "*.html" {
const content: string;
export default content;
}
在app.component.ts中
In 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'.有人有解决方法吗?
The app works and the template is loaded but I get this error TS2352: Type 'typeof "*.html"' cannot be converted to type 'string'. Does someone have a workaround?
我想这样做.
import template from './app.component.html';
@Component({
template: template
})
...
但是转码的代码最终会这样.
But the transpiled code ends up like this.
var app_component_html_1 = __webpack_require__(653);
...
template: app_component_html_1.default
app_component_html_1是我需要的字符串.他们为什么要添加.default?
app_component_html_1 is the string I need. Why do they add the .default?
推荐答案
将export default content;
修改为export = content;
可以解决此打字稿错误.
Modify export default content;
to export = content;
resolved this typescript error.
之前
之后
并且已编译的代码应能正常工作(因为删除了默认").
And the transpiled code should works accordingly (since 'default' is removed).
这篇关于Typescript 2通配符模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!