vue编译图片不显示,require()与require().default区别

vue打包图片,图片地址不存在或者出现[object module],或者图片地址存在排查方式

vue 编译图片

vue编译图片loader有两种file-loader、url-loader。url-loader将图片编译为base64,file-loader编译为地址访问的图片,通常两种混用,通过limit控制base64与文件打包。

vue.config.js
chainWebpack(config) {
    config.module.rule("images").use("url-loader").loader("url-loader").tap(options => {
      return {
        ...options,
        limit: 4096,
      };
    }).end();
}
webpack.config.js
module: {
    rules: [{
        test: /\.(svg|gif|png|jpe?g)(\?\S*)?$/,
        loader: 'url-loader',
        options: {
          limit: 4096,
        },
      }]
}

require() 与require().default图片区别

我们可以将一张图片通过loader编译为一个js文件,nodejs使用require引入一个js导出的信息,而require().default 就是获取default属性或者默认导出信息。

当使用require().default或者requre()引入图片不生效时,那就是使用的方式与打包导出方式没有对应上。
在这里插入图片描述

通常遇到的情况是上面这种情况 [object Module], 这表示找到的是一个对象,这是发现使用require().default就能找到图片,但是我们通常希望不加default。需要配置esmodule

// vue.config.js
chainWebpack(config) {
  config.module.rule("images").use("url-loader").loader("url-loader").tap(options => {
    return {
      ...options,
      limit: 4096,
      fallback: {
        loader: "file-loader",
        options: {
          name: "static/img/[name].[ext]",
          // 是否模块化
          esModule: false,
        },
      },
      // 是否模块化
      esModule: false,
    };
  }).end();
}
// webpack.config.js
module: {
    rules: [{
        test: /\.(svg|gif|png|jpe?g)(\?\S*)?$/,
        loader: 'url-loader',
        options: {
          fallback: {
            loader: 'file-loader',
            options: {
              name: path.posix.join(`img`, '[name].[ext]'),
              // 是否模块化
              esModule: false,
            },
            // 是否模块化
          	esModule: false,
          },
          limit: 4096,
        },
      }]
}

在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/732cadd3907146ca91498cf119d11eec.png

出现以上问题检查图片编译 esModule的值,若为false,将require()的default属性去掉。

综上所述,如果使用require().default, 就不需要加esModule属性,或者esModule:true;若使用require()需要配置esModule:false。 (file-loader 4.2.0以上)

Vue项目中,要正确配置webpack以使用require.context进行动态导入,你需要按照以下步骤操作: 1. **理解require.context**:require.context是一个webpack编译时函数,允许你创建自己的上下文,用于动态导入。你可以指定一个目录,是否搜索子目录,以及匹配文件的正则表达式。 2. **配置webpack**:在webpack的配置文件(通常是webpack.config.js)中,你需要确保有两个配置选项是启用的: - **context**:设置为你的项目源文件的根目录。 - **resolve.alias**:这可以用来定义模块的别名,使得require.context可以更方便地使用。 3. **使用require.context**:在你的Vue组件或JavaScript文件中,你可以使用require.context来创建上下文,并使用它的.keys()和.resolve()方法动态地加载模块。 以下是一个简单的配置示例: ```javascript // 在webpack.config.js中 const path = require('path'); module.exports = { //... resolve: { alias: { // 设置别名,例如: '@': path.resolve(__dirname, 'src'), // 其他别名... }, }, //... }; // 在Vue组件或JavaScript文件中使用require.context const files = require.context('路径', 是否递归搜索, 正则表达式); const modules = files.keys().reduce((modules, modulePath) => { const key = modulePath.replace(/(\.\/|\.js)/g, ''); modules[key] = files(modulePath); return modules; }, {}); ``` 这里是一个具体的使用场景: ```javascript // 假设我们有一个目录结构如下: // components/ // ├── Button.vue // ├── Icon.vue // └── Layout/ // ├── Header.vue // ├── Footer.vue // 我们想动态加载components目录下的所有组件 const requireComponent = require.context('./components', false, /\.vue$/); const components = requireComponent.keys().reduce((components, filePath) => { const componentConfig = requireComponent(filePath); const componentName = filePath.split('/').pop().split('.')[0]; components[componentName] = componentConfig.default || componentConfig; return components; }, {}); // 现在可以在Vue实例中这样使用components对象: // components: { // Button: components.Button, // Icon: components.Icon, // LayoutHeader: components.LayoutHeader, // LayoutFooter: components.LayoutFooter // } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值