我在导入我的angular 2应用程序中的html模板时遇到了麻烦。Typescript 2通配符模块
在
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');
}
}
该应用的工作原理和模板加载globals.d.ts但我得到这个错误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?
你为什么这么做?为什么不只是'templateUrl:'。/ app.component.html''? – jonrsharpe
正在测试角2 a,并使用templateUrl。但发现它很难用于webpack。需要额外的装载机和步骤来使其工作。 我以为只使用模板:会更容易。 xD –
只是想说明我在TS 2.2中遇到同样的问题。但是,我的html导入使用ng-cache-loader和webpack解析为模板缓存键。我有从'./header.template.html';'import *作为templateUrl,但是当我去使用'templateUrl: templateUrl'指定一个指令时,我得到了与OP所述的相同的错误。 –