SVG loading vue-svg-loader; [Vue warn]: Invalid Co

2020-06-17 14:48发布

If I want to use vue-svg-loader in an existing vue-cli application, I get the error message

[Vue warn]: Invalid Component definition: /img/logout.b0c3dfb7.svg

Following Steps are already done:

1) Install vue-svg-loader as devDependency

2) Add Webpack Config in vue.config.js (root directory):

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.svg$/,
          loader: 'vue-svg-loader', 
        },
      ],
    }      
  }
};

3) Import SVG and inject as Component

import Logout from '@/assets/img/icons/logout.svg';

export default {
    components: {
      Logout,
    },
...
}

4) Use it in the template (vuetify as UI-Framework)

<v-btn icon @click="logout()">
    <Logout class="icon" />
</v-btn>

3 Questions:

  1. What is my mistake?
  2. How/where can/should I add new Webpack settings
  3. or modify/overwrite/delete existing ones

in a vue-cli (V3) project?

2条回答
Emotional °昔
2楼-- · 2020-06-17 14:53

In webpack.base.conf.js add the following code under rule:

 {
    test: /\.svg$/,
    loader: 'vue-svg-loader',
  }

And remove the svg from the existing below rule:

{
    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('img/[name].[hash:7].[ext]')
    }
 }
查看更多
做个烂人
3楼-- · 2020-06-17 14:56

Add the following into vue.config.js:

module.exports = {
  chainWebpack: config => {
    config.module.rules.delete("svg");
  },
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.svg$/, 
          loader: 'vue-svg-loader', 
        },
      ],
    }      
  }
};
查看更多
登录 后发表回答