首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue:使用material-design-icons offline / size

Vue:使用material-design-icons offline / size
EN

Stack Overflow用户
提问于 2019-02-08 22:31:06
回答 1查看 3.9K关注 0票数 3

我在我的vue项目中使用了materialdesignicons。

代码语言:javascript
复制
require ('../node_modules/@mdi/font/css/materialdesignicons.min.css);
Vue.use(Vuetify, {iconfont:'mdi'});

我有几个动态创建的图标:

代码语言:javascript
复制
 <v-icon>{{ some-mdi-file }}</v-icon>

当我通过(npm run build)为生产环境进行构建时,我得到了以下错误:

代码语言:javascript
复制
asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
Assets:
  img/materialdesignicons-webfont.someHash.svg (3.77 MiB)

这个文件的大小很大,因为它包含了每个图标,无论它是否正在使用。有没有一种方法可以通过只打包使用的特定图标来减小文件大小。有没有我应该使用的不同的包?注意:该项目是离线托管的,所以我需要在我的项目中直接包含字体。

我查看了vue-material-design-icons,但它看起来可能不适用于动态图标名称,并且它没有说明总体文件大小/性能。

我也看过这里,但单击'size warning‘链接会将我带到一个页面,其中的Vue部分没有填写https://dev.materialdesignicons.com/getting-started/webfont

EN

回答 1

Stack Overflow用户

发布于 2019-05-31 17:37:48

为此,我建议使用@mdi/js包,它为每个图标提供SVG路径,并支持树摇动。目前Vuetify还不支持SVG图标,但支持it should in the future

现在,创建一个定制的图标组件已经很简单了:

代码语言:javascript
复制
<template>
    <svg :class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
        <path :d="path" />
    </svg>
</template>

<script>
export default {
    name: 'my-icon',

    data: () => ({
        path: '',
    }),

    methods: {
        updatePath() {
            if (!this.$scopedSlots) return
            if (typeof this.$scopedSlots.default !== 'function') return

            this.path = this.$scopedSlots
                .default()
                .map((n) => n.text)
                .join('')
        },
    },

    mounted() {
        this.updatePath()
    },

    updated() {
        this.updatePath()
    },
}
</script>

<style scoped>
.icon {
    display: block;
    color: inherit;
    fill: currentColor;
    width: 24px;
    height: 24px;
}
<style>

然后要使用它,你只需要导入你的组件和你想要使用的图标:

代码语言:javascript
复制
<template>
    <div class="app">
        <my-icon>{{mdiCheck}}</my-icon>
    </div>
</template>

<script>
import MyIcon from 'path/to/my/icon.vue'
import { mdiCheck } from '@mdi/js'

export default {
    name: 'my-app',

    components: {
        MyIcon,
    }

    data: () => ({
        mdiCheck,
    }),
}
</script>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54594565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档