Typescript 2 wildcard module

早过忘川 提交于 2019-12-11 06:34:18

问题


I am having trouble with importing html templates in my angular 2 app.

In globals.d.ts

 declare module "*.html" {
    const content: string;
    export default content;
}

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');
    }
}

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?

I would like to do this instead.

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 is the string I need. Why do they add the .default?


回答1:


Modify export default content; to export = content; resolved this typescript error.

Before

After

And the transpiled code should works accordingly (since 'default' is removed).



来源:https://stackoverflow.com/questions/42829579/typescript-2-wildcard-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!