1

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

4

1 回答 1

5

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

不行

工作过

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

于 2017-03-22T08:57:04.680 回答