Angularjs 在typescript中导入html模板
我正在尝试导入我的html模板,以便webpack能够识别它们,并在构建时包含它们。(Angularjs 在typescript中导入html模板,angularjs,typescript,angular-routing,webpack-3,typescript-2.5,Angularjs,Typescript,Angular Routing,Webpack 3,Typescript 2.5,我正在尝试导入我的html模板,以便webpack能够识别它们,并在构建时包含它们。(webpack-d) 根据法律,我应该这样做 declare module '*.html' { const _: string; export default _; } 然后从“/myTemplate.html”导入模板应该可以工作 但这并不完全起作用 import template from './myTemplate.html'; console.log(template); // un
webpack-d
)
根据法律,我应该这样做
declare module '*.html' {
const _: string;
export default _;
}
然后从“/myTemplate.html”导入模板代码>应该可以工作
但这并不完全起作用
import template from './myTemplate.html';
console.log(template); // undefined
然而,这“有效”
但是,如果我将*.html模块更改为
declare module '*.html' {
const _: string;
export = _; // changed this
}
我现在可以做了
import * as template from './myTemplate.html';
$routeProvider.when("/test", {
template: template, // Great success!
controller: MyController,
controllerAs: "$ctrl"
});
它可以工作,但是为什么不从“/myTemplate.html”导入模板呢代码>工作我做错了什么因为GitHub问题中的其他人似乎让它这样工作。为了使用“/myTemplate.html”导入模板,您没有做错任何事您需要使用
导出默认值
语法
因为webpack是处理html
导入的,所以默认情况下导出
但是您可以使用
html加载程序使用导出默认值。Aha,因此这实际上是一个网页问题,而不是一个typescript问题。这不是一个配置问题。
declare module '*.html' {
const _: string;
export = _; // changed this
}
import * as template from './myTemplate.html';
$routeProvider.when("/test", {
template: template, // Great success!
controller: MyController,
controllerAs: "$ctrl"
});