我想知道是否可以像標題所說的那樣做。是否可以使用TypeScript將HTML文件作爲字符串導入?
例如,假設我們正在開發一個Angular2項目,並且我們希望避免將模板設置爲外部url,以減少http請求。我們仍然不想在組件中編寫所有的HTML,因爲它可能足夠大,或者我們希望設計人員使用不同於開發人員的文件工作。
所以這是第一個解決方案:
文件template.html.ts 變換一個文件的.ts到這樣的事情:
export const htmlTemplate = `
<h1>My Html</h1>
`;
在我的部分,我可以
然後像這樣導入它:
import { Component } from 'angular2/core';
import {RouteParams, RouterLink} from 'angular2/router';
import {htmlTemplate} from './template.html';
@Component({
selector: 'home',
directives: [RouterLink],
template: htmlTemplate,
})
其實這個工作每很遺憾,但是你失去了IDE的HTML智能,所以這對創建HTML模板的設計器/ dev是不利的。
我試圖做到的,是找到一種方法來導入的.html文件,而不是.TS。
那麼有可能在TypeScript中將一個.html文件作爲字符串導入?
我做了什麼來實現這一目標是在webpack中使用'require',創建一個html文件,然後使用'template:require('component.html')',它將作爲一個字符串導入。 –
是的,這是一種方法。感謝你的加入,我也會和你一起玩,我會讓你知道我的想法。 –
不是你正在尋找的解決方案,我使用gulp-angular-embed-templates在構建時在我的js類中嵌入html模板 – brando