请检查此https://github.com/rails/webpacker/blob/master/docs/typescript.md#html-templates-with-typescript-and-angular
yarn add html-loader
将 html-loader 添加到 config/webpack/environment.js
environment.loaders.set('html', {
test: /\.html$/,
use: [{
loader: 'html-loader',
options: {
minimize: true,
removeAttributeQuotes: false,
caseSensitive: true,
customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
customAttrAssign: [ /\)?\]?=/ ]
}
}]
})
将 .html 添加到 config/webpacker.yml
extensions:
- .elm
- .coffee
- .html
设置自定义 d.ts 定义 // app/javascript/hello_angular/html.d.ts
declare module "*.html" {
const content: string
export default content
}
添加相对于 app.component.ts 的 template.html 文件
<h1>Hello {{name}}</h1>
Import template into app.component.ts
import { Component } from '@angular/core'
import templateString from './template.html'
@Component({
selector: 'hello-angular',
template: templateString
})
export class AppComponent {
name = 'Angular!'
}