Typescript 2 通配符模块

Typescript 2 wildcard module

我在 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?

export default content; 修改为 export = content; 解决了这个打字稿错误。

之前

之后

并且转译后的代码应该相应地工作(因为 'default' 被删除)。