问题
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