Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FontAwesome SVG icons with Vuetify - how to use within v-icon/prepend-icon?

I'm new to Vue, can't find the exact answer how to use FA SVG icons in v-icon and prepend-icon. If i use:

<font-awesome-icon :icon="dekstop" color="gray"></font-awesome-icon>

Icons are displayed, but how i can use FA SVG icons in v-icon and prepend-icon? Sample:

            <v-autocomplete v-model="replUser"
            :items="users" 
            color="accent white--text" 
            label="User"
            prepend-icon="dekstop></i>" // i can use material font icons here, but FA SVG not worked
            >

            </v-autocomplete>

my main.js:

import Vue from 'vue'
import Vuetify from 'vuetify/lib'
// import colors from 'vuetify/es5/util/colors'
import './plugins/vuetify'
import App from './App.vue'
import i18n from './i18n'
import {
  library
} from '@fortawesome/fontawesome-svg-core'
import {
  FontAwesomeIcon
} from '@fortawesome/vue-fontawesome'
import {
  fas
} from '@fortawesome/free-solid-svg-icons'

Vue.component('font-awesome-icon', FontAwesomeIcon) // Register component globally
library.add(fas) // Include needed icons.

Vue.use(Vuetify, {
  iconfont: 'faSvg',
})
Vue.config.productionTip = false

new Vue({
  i18n,
  render: h => h(App)
}).$mount('#app')

What am I doing wrong?

like image 969
n4ks Avatar asked Mar 17 '19 12:03

n4ks


People also ask

How do I use material icons in Vuetify?

The v-icon component provides a large set of glyphs to provide context to various aspects of your application. For a list of all available icons, visit the official Material Design Icons page. To use any of these icons simply use the mdi- prefix followed by the icon name.

How do I add material design icons in Vuetify?

Material Design Icons - JS SVGUse the SVG paths as designated in @mdi/js . This is the recommended installation when optimizing your application for production. You ONLY need to include this if you plan on using more than the default icons.


1 Answers

If you want to use svg icons in v-icon/prepend-icon, then you need to access them through $vuetify.icons object. Any other text in the v-icon/prepend-icon will be interpreted as webfont/css class.

But for use $vuetify.icons object you should register custom icons. Example:

import Vue from "vue";
import Vuetify from "vuetify";
import { library } from "@fortawesome/fontawesome-svg-core";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { faVuejs } from "@fortawesome/free-brands-svg-icons";

Vue.component("font-awesome-icon", FontAwesomeIcon); // Register component globally
library.add(faVuejs); // Include needed icons.

Vue.use(Vuetify, {
  icons: {
    vue: {
      component: FontAwesomeIcon,
      props: {
        icon: ["fab", "vuejs"]
      }
    }
  }
});

And now you can use vue-brand icon:

<v-icon>$vuetify.icons.vue</v-icon>
<v-text-field prepend-icon="$vuetify.icons.vue"/>

Also, you can use without register via font-awesome-icon:

<font-awesome-icon :icon="['fab', 'vuejs']" />
<v-text-field>
    <font-awesome-icon :icon="['fab', 'vuejs']" slot="prepend"/>
</v-text-field>

Out of the box vuetify has a preset of icons config that can be associated with SVG:

...
import { fas } from "@fortawesome/free-solid-svg-icons";
...
library.add(fas);
...
Vue.use(Vuetify, {
  iconfont: "faSvg"
});

But for use it you should call $vuetify.icons again (vuetify just wrap preset as component):

<v-icon>$vuetify.icons.warning</v-icon>

Full example codesandbox

like image 106
Nikolay Votintsev Avatar answered Sep 28 '22 12:09

Nikolay Votintsev