angularjs在webpack下使用require引用户html文件时,出现 module.exports = "\n

缘由猜测:使用html-loader加载了两次htmlhtml

好比,错误示例:app

module: {
        rules: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            {test: /\.ts$/, loader: "ts-loader"},
            {test: /\.(html)$/, loader: "html-loader"},
            {test: /\.html/, loader: "html-loader"}
        ]
    },

则在代码中看到require('./yunzhi.html')时。首先,因为符合第一条 {test: /\.(html)$/, loader: "html-loader"},则将html编译为变量A -> (module.exports);而后,因为再次符合第二条规则{test: /\.html/, loader: "html-loader"},又从新将A进行了二次编译,而后就出现了咱们不想看到的。ui

例子

a.htmlspa

<yunzhi></yunzhi>

yunzhi组件code

app.component('yunzhi', {
    template: require('./yunzhi.html')
    ...
});

yunzhi.htmlcomponent

<h1>hello</h1>
<h2>hello</h2>

则发生以下错误:htm

clipboard.png

解决方案

删除冗余的loader
删除前:ip

module: {
        rules: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            {test: /\.ts$/, loader: "ts-loader"},
            {test: /\.(html)$/, loader: "html-loader"},
            {test: /\.html/, loader: "html-loader"}
        ]
    },

删除后:it

module: {
        rules: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            {test: /\.ts$/, loader: "ts-loader"},
            {test: /\.(html)$/, loader: "html-loader"},
        ]
    },
相关文章
相关标签/搜索