vue3.0 引入svg-icon组件

1。在vue项目中的components添加目录SvgIcon:

vue3.0 引入svg-icon组件_第1张图片

2. 在目录下添加Index.vue,如上图; Index.vue的内容为:





其中第三行的写法: v-on="$attrs" 为vue3.0写法

3.在 src/assets新建立目录:icons, 在其下建立子目录 svg, 在svg下拷贝user.svg图标文件:

vue3.0 引入svg-icon组件_第2张图片

 

同时在 icons 还要建立两个文件index.js 和 svgo.yml文件,其内容分别:

index.js内容:

import { createApp } from 'vue';
import SvgIcon from '@/components/SvgIcon/Index'  // svg component

// register globally
createApp({}).component('svg-icon', SvgIcon) //vue3.0的写法

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

svgo.yml的内容为:

# replace default config

# multipass: true
# full: true

plugins:

  # - name
  #
  # or:
  # - name: false
  # - name: true
  #
  # or:
  # - name:
  #     param1: 1
  #     param2: 2

  - removeAttrs:
      attrs:
        - 'fill'
        - 'fill-rule'

4. 最后在main.js中引入组件:

vue3.0 引入svg-icon组件_第3张图片

 

5. 最后是使用组件:

vue3.0 引入svg-icon组件_第4张图片

 

你可能感兴趣的:(vue.js,javascript,前端)