【发布时间】:2017-01-16 14:10:43
【问题描述】:
This discussion 建议在typescript 2 中,可以将外部的非css 文件作为字符串导入我的班级。
我正在尝试利用这一点将 HTML 导入到我的 typescript 组件中,以便我的应用程序将发出一个 HTTP 请求来获取组件及其 HTML 模板,而不是两个单独的请求。我试过了:
typings.d.ts
declare module "*!text" {
const value: any;
export default value;
}
app.component.ts
import htmlTemplate from './app.component.html!text';
...
constructor(){console.log(htmlTemplate)}
打字稿编译器不会引发错误。但是,当我运行该应用程序时,它在请求 http://localhost/text 并获得 404 not found 后崩溃`
我也试过: typings.d.ts
declare module "*.html" {
const value: any;
export default value;
}
app.component.ts
import htmlTemplate from './app.component.html';
...
constructor(){console.log(htmlTemplate)}
在编译期间再次没有引发错误,但应用程序请求 http://localhost/app.component.html.js 并在收到 404 not found 响应后崩溃。
模板位于app.component.html 中,与.ts 文件位于同一文件夹中。我正在运行typescript 2.0.2。如何将我的.html 模板和.css 样式表导入为字符串?
【问题讨论】:
标签: typescript module