正文
TypeScript 文件引入 Html (ts import html webpack)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
我们的目标是把html引入ts文件,webpack打包时就能把html打进js文件,减少文件加载啦
1 安装 text-loader
npm install text-loader --save-dev
2 webpack 配置里新增 text-loader 为文本加载器
module: {
rules: [
{
test: /\.html$/,
use: 'raw-loader',
}
]
新建文件 命名为 typings.d.ts, 这样TypeScript文件里就可把Html引入为字符串模块了
declare module "*.html" {
const content: string;
export default content;
}
4 在 TypeScript 里引入html
import prompt from '../../dom/Prompt.html';(这里写html文件的相对路径)
console.log( prompt ) ;