在Vue 3.0中使用填充和轮廓版本的Google Material Design图标字体,结合Vuetify 3.0。

huangapple go评论46阅读模式
英文:

Use filled and outlind version of Google Material Design Icon Font in vue 3.0 with vuetify 3.0

问题

I want to use Google Material Design Icon Font in my vue3.0 project where I also use vuetify 3.0.

The steps I followed to achieve this are:

  1. In main.ts file, set the default icon to md (imported from vuetify iconsets)

  2. Use google icon name in the vuetify component v-btn. Here, picture_as_pdf is the name of google material disign icon.

  3. It will show you a filled version of icon.

Everything is good till now. But I want to use both outlined as well as filled version of google material design icon.

I have solution for this problem but it is not working with vuetify component. Does anyone have any solution for this?

Sharing my solution with you all which is working fine with normal html tags but not working with vuetify components.

Steps:

  1. Add the cdn link (for filled and outlined both) to the html file

  2. In the component template

英文:

I want to use Google Material Design Icon Font in my vue3.0 project where I also use vuetify 3.0.

The steps I followed to achieve this are:

  1. In main.ts file, set the default icon to md (imported from vuetify iconsets)

    在Vue 3.0中使用填充和轮廓版本的Google Material Design图标字体,结合Vuetify 3.0。

  2. Use google icon name in the vuetify component v-btn. Here, picture_as_pdf is the name of google material disign icon.

     <v-btn
        rounded="pill"
        class="bg-lightgrey ma-2"
        prepend-icon="picture_as_pdf"
        @click="emit('generateReport')"
      >
        Export
      </v-btn>
    
  3. It will show you a filled version of icon.

Everything is good till now. But I want to use both outlined as well as filled version of google material design icon.

I have solution for this problem but it is not working with vuetify component. Does anyone have any solution for this?

Sharing my solution with you all which is working fine with normal html tags but not working with vuetify components.

Steps:

  1. Add the cdn link (for filled and outlined both) to the html file

    <link
      href="https://fonts.googleapis.com/css? 
      family=Material+Icons|Material+Icons+Outlined"
      rel="stylesheet"
        />
    
  2. In the component template

    <span class="material-icons">face</span> 
    <span class="material-icons-outlined">face</span>
    

NOTE: It worked well but when I use it in vuetify component let's say v-btn, it is not working.

答案1

得分: 1

你可以在Vuetify配置中注册一个自定义图标集渲染器:

const vuetify = createVuetify({
  icons: {
    defaultSet: 'mdio',
    sets: {
      mdio: {
        component: props => h(VLigatureIcon, {
          ...props,
          class: 'material-icons-outlined' // <---- 将类设置为你的轮廓类
        })
      }
    }
  }
})

VLigatureIcon 组件是Vuetify的半内部组件,功能不多,所以如果不适用于你,你可以很容易地编写自己的组件。

但对于轮廓字体,你可以参考以下示例:

<!-- 开始代码片段: js 隐藏: true 控制台: false Babel: false -->

<!-- 语言: lang-js -->

const { createApp, ref, h } = Vue;
const { createVuetify } = Vuetify;

const vuetify = createVuetify({
  icons: {
    defaultSet: 'mdio',
    sets: {
      mdio: {
        component: props => h(Vuetify.components.VLigatureIcon, {
          ...props,
          class: 'material-icons-outlined'
        })
      }
    }
  }
});

const app = {
  setup() {
    return {
      icons: ref(['add', 'open_in_new', 'indeterminate_check_box', 'view_timeline', 'create_new_folder'])
    };
  }
};

createApp(app).use(vuetify).mount('#app');

<!-- 语言: lang-html -->

<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css" />
<link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet" />

<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-row align="center" justify="center">
          <v-col cols="auto" v-for="icon in icons" :key="icon">
            <v-btn :icon="icon"></v-btn>
          </v-col>
        </v-row>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js"></script>

<!-- 结束代码片段 -->

英文:

You can register a custom iconset renderer in the Vuetify configuration:

const vuetify = createVuetify({
  icons: {
    defaultSet: &#39;mdio&#39;,
    sets: {
      mdio: {
        component: props =&gt; h(VLigatureIcon, {
          ...props,
          class: &#39;material-icons-outlined&#39; // &lt;---- set class to your outline class 
        })
      }
    }
  }
})

The VLigatureIcon Component is a semi-internal Vuetify component, it doesn't do much, so you can easily write your own if it does not work for you.

But for the outlined font, it does, have a look at the snippet:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

const {createApp, ref, h} = Vue;
const {createVuetify} = Vuetify

const vuetify = createVuetify({
  icons: {
    defaultSet: &#39;mdio&#39;,
    sets: {
      mdio: {
        component: props =&gt; h(Vuetify.components.VLigatureIcon, {
          ...props,
          class: &#39;material-icons-outlined&#39;
        })
      }
    }
  }
})

const app = {
  setup() {
    return {
      icons: ref([&#39;add&#39;, &#39;open_in_new&#39;, &#39;indeterminate_check_box&#39;, &#39;view_timeline&#39;, &#39;create_new_folder&#39;])
    }
  }

}
createApp(app).use(vuetify).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; /&gt;
&lt;link href=&quot;https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;

      &lt;v-container&gt;
        &lt;v-row align=&quot;center&quot; justify=&quot;center&quot;&gt;
          &lt;v-col cols=&quot;auto&quot;v-for=&quot;icon in icons&quot; :key=&quot;icon&quot;&gt;
            &lt;v-btn :icon=&quot;icon&quot;&gt;&lt;/v-btn&gt;
          &lt;/v-col&gt;
        &lt;/v-row&gt;
      &lt;/v-container&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 18:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定