Typescript 2 通配符模块

Lar*_*sen 1 module wildcard typescript2.2 angular

我在 angular 2 应用程序中导入 html 模板时遇到问题。

在 globals.d.ts

 declare module "*.html" {
    const content: string;
    export default content;
}
Run Code Online (Sandbox Code Playgroud)

在 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');
    }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序可以工作并且模板已加载,但我收到此错误 TS2352:类型 'typeof "*.html"' 无法转换为类型 'string'。有人有解决方法吗?

我想这样做。

import template from './app.component.html';

@Component({
    template: template
})
...
Run Code Online (Sandbox Code Playgroud)

但转译后的代码最终是这样的。

var app_component_html_1 = __webpack_require__(653);
...
template: app_component_html_1.default
Run Code Online (Sandbox Code Playgroud)

app_component_html_1 是我需要的字符串。他们为什么要添加 .default?

Val*_*Val 5

修改export default content;export = content;解决此打字稿错误。

不行

工作过

并且转译的代码应该相应地工作(因为删除了“默认”)。