You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue CLI+Webpack环境下实现<object>标签data属性路径编译需求

Fixing Webpack Compilation for <object> Tag data Attribute in Vue CLI

Hey there, I’ve dealt with this exact problem while working with Vue CLI and Webpack—let’s break down why it happens and how to fix it quickly.

Why the data Attribute Isn’t Being Compiled

By default, Vue’s vue-loader (which Webpack uses under the hood in Vue CLI) only automatically processes specific attributes on certain tags for asset path resolution—like src on <img>, <video>, etc. The data attribute on <object> isn’t in that default list, so Webpack ignores it.

Solution 1: Manual require() for Individual Assets

If you only need this for a few <object> tags, the quickest fix is to use Vue’s dynamic binding with require() to tell Webpack to process the path:

<object type="image/svg+xml" :data="require('../path/to/file.svg')"></object>

The colon (:) before data makes it a dynamic binding, and require() triggers Webpack’s asset resolution just like it does for <img src="...">.

Solution 2: Configure vue-loader to Process data Globally

If you want all <object> tags’ data attributes to be handled automatically (just like <img>), you can update your Vue CLI config to add data to vue-loader’s asset processing list.

For Vue CLI 3+ (using vue.config.js):

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        // Extend the default transformAssetUrls to include <object> -> data
        options.transformAssetUrls = {
          ...options.transformAssetUrls,
          object: 'data'
        }
        return options
      })
  }
}

For older Vue CLI 2 projects, you’ll find the vue-loader config in build/webpack.base.conf.js—look for the vue-loader entry and modify the transformAssetUrls option there the same way.

Once you save this config and restart your dev server, your <object data="../path/to/file.svg"> tags will have their paths compiled just like <img> tags do!

内容的提问来源于stack exchange,提问作者kayza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:34:54