加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

在webpack中导入html文件时找不到模块错误

发布时间:2020-12-14 19:42:01 所属栏目:资源 来源:网络整理
导读:尝试使用 html-loader插件在TypeScript中导入html: import buttonHtml from './button.html'; 出现TypeScript错误: TS2307: Cannot find module ‘./button.html’ Webpack配置: const path = require('path');module.exports = { entry: { 'background.j
尝试使用 html-loader插件在TypeScript中导入html:
import buttonHtml from './button.html';

出现TypeScript错误:

TS2307: Cannot find module ‘./button.html’

Webpack配置:

const path = require('path');

module.exports = {
  entry: {
      'background.js':path.resolve(__dirname,'./background.ts'),'content.js': path.resolve(__dirname,'./content.ts')
  },devtool: 'inline-source-map',module: {
    rules: [
      {
        test: /.tsx?$/,use: 'ts-loader',exclude: /node_modules/
      },{
        test: /.html$/,exclude: /node_modules/,use: {loader: 'html-loader'}
    }
    ]
  },resolve: {
    extensions: [ ".tsx",".ts",".js" ]
  },output: {
    filename: '[name]',path: path.resolve(__dirname,'dist')
  }
};

解决方法

如果你想加载这样的html,你需要一个带有这个条目的typings.d.ts文件:
declare module '*.html' {
  const value: any;
  export default value;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读