Using dynamic mdi svg icons with Vuetify

I'm struggling to display a dynamic v-icon within a v-select component.
I want to replace the icon name with its imported value

            <v-select v-model="data.type" dense :items="typeItems" :label="$tc('Type')">
              <template v-slot:item="data">
                <v-list-item>
                  <v-list-item-icon>
                    <v-icon>{{data.item.icon}}</v-icon>
                  </v-list-item-icon>
                  <v-list-item-content>
                    <v-list-item-title v-html="data.item.text"></v-list-item-title>
                  </v-list-item-content>
                </v-list-item>
              </template>
            </v-select>

The value of data.item.icon is a string like "mdiArrowLeft".

To clarify, mdiArrowLeft has been imported as: import {mdiArrowLeft} from "@mdi/js" and then returned in setup(). I am utilizing the composition API.

Despite my efforts, substituting {{data.item.icon}} with its imported value hasn't worked out
The outcome on the screen remains mdiArrowLeft instead of the actual icon.

Answer №1

src/plugins/vuetify.js

Set the mdiSvg iconfont:

import Vue from 'vue'
import Vuetify from 'vuetify/lib'

Vue.use(Vuetify)

export default new Vuetify({
  icons: {
    iconfont: 'mdiSvg',
  },
})

VUE Component

...
<v-icon>{{retrieveIcon(data.item.icon)}}</v-icon>
...

<script>
  export default {
    methods: {
      async retrieveIcon (iconName) {
        return { [iconName]: icon } = await import('@mdi/js')
      }
    }
  }
</script>

Answer №2

Upon receiving advice from @Alex Hoffman, I was able to devise a solution:

<v-icon small>{{ chooseIcon(data.item.value)}}</v-icon>
...
    function chooseIcon(type) {
      if (type === 1) return mdiArrowLeft;
      if (type === 2) return mdiCurrencyEur;
      if (type === 3) return mdiArrowRight;
      if (type === 4) return mdiEmailOutline;
      if (type === 5) return mdiCloudUploadOutline;
      if (type === 6) return mdiDeleteForever;
      if (type === 7) return mdiPhone;
      if (type === 8) return null;
      if (type === 9) return mdiDownload;
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Vue.js to submit a form in Laravel and redirecting with a flash message

I am facing an issue where I have two components named Index and Create, loaded from separate blade files. The challenge is passing a flash message as a prop between these components due to their file separation. How can I redirect after submitting a form ...

Axios does not appear to be transmitting cookies

In my development setup, I am using two separate applications: a server-side application built with Laravel and a client-side application using VueJS. The Vue app consumes the API provided by the Laravel app to interact with the backend functionalities. A ...

Transmitting document through HTML form using Laravel integration with Mailgun API

I'm currently working on a feature where users can select a file via an HTML form and have it sent as an email attachment using Mailgun. I want to avoid storing the file on my server. The goal is to allow for uploading various file types such as PNG, ...

When working with Vuex, remember to only alter the store state within mutations. Avoid mutating the Vuex

Consider the following situation: Textfield Component: <v-text-field v-model="form.profile.mobile_business" label="Mobile" prepend-inner-icon="mdi-cellphone" ></v-text-field> To retrieve the current value, I use: data() { retu ...

Navigating coordinates with mapbox

Is mapbox only able to support coordinates in the format of numbers such as [-58.5003038, -34.5741957]? In my database, I have coordinates stored in a format containing both numbers and directions like 1010N 09710W. When I try to directly place them on th ...

Issue encountered while attempting to deactivate button until numerical data is entered in the OTP field using Vuejs

otp_value: '', isFadeout: false, verifyOtp() { this.disabled = true; this.otpBtnClicked = false; this.verified = true; }, <input class="o ...

What is the best way to transfer variables between two Vue files?

How can I transfer a variable between two Vue files? Hello, in my SendCode.vue file, I have a variable named NewEmail that I would like to use in another file called changePass.vue. Is there any way to do this? Can someone offer assistance? Thank you</p ...

Obtain the shared value between two data entries within a Vue array

In my Vue.js data, I have two records with the same question id. I want to select both record options and only one of the questions. [ { "id":7, "question_id":102, "option":"true", "is_corr ...

"When the value is false, use the Vue binding to apply a specific

My challenge involves managing a website that is designed to receive boolean values and store them in an array where the key represents the ID and the value represents the boolean. For example: policiesActive[ "2" => false, "3" => false] The w ...

Component fails to trigger @click handler

.vue component <template> <div class="modal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> Loading Files </div> < ...

When attempting to define a route as a lambda function in vue-cli, the default lazy load code does not function

After using vue-cli to set up a new Typescript project with vue-router included, I noticed that the generated router/index.ts configuration looked like this: const routes: Array<RouteConfig> = [ { path: '/', name: 'Home' ...

The system was unable to locate node.js with socket.io

I'm having trouble locating the file. According to the documentation I reviewed, socket.io is supposed to be automatically exposed. Encountered Error: polling-xhr.js?bd56:264 GET http://localhost:8081/socket.io/?EIO=3&transport=polling&t=LyY ...

What is the process for integrating a component into this Vue component?

If we are looking to incorporate a component from into the following code snippet, things get a bit tricky. The provided example is not tailored for decorator definitions. So, where exactly should the component list be placed within this script? <sc ...

Decoding user input parameters within a vue module

It seems like I am hitting a wall when it comes to finding solutions for this particular issue. Currently, I have a component that is supposed to retrieve data from a file and display it. My intention is to only pass the filename to the component so that ...

What is the best way to verify changing input fields in vue.js?

Validation of input fields using vuelidate is essential. The input field in question is dynamic, as the value is populated dynamically with jsonData through the use of v-model. The objective: Upon blur, the goal is to display an error if there is one; ho ...

What is the best way to modify the size of an SVG element within Vuetify?

Hi there, I'm currently a PHP developer who is delving into the world of Vue. I'm curious about how to adjust the height and width of an SVG image using the v-img tag. Below is the code snippet: <v-img src="/empty.svg" aspect-ratio="2">< ...

Storing various data entries within a MySQL database through the use of Vuex

I am currently facing an issue with inserting multiple data into my database using Vuex. Despite only inserting two data points, I keep getting the error message: COLUMN DOESN'T MATCH VALUE COUNT AT ROW 1. https://i.stack.imgur.com/qBcvK.png Below i ...

Redis: Unable to establish a connection as net.connect is not recognized as a

I'm struggling to integrate Redis with nodejs Encountering issues during execution Despite using the same code, I am facing this error: Here's my code snippet: import { createClient } from 'redis' export const client = createClient({ ...

Utilizing the Power of GrapesJs in Vue3

Recently, I attempted to integrate the GrapesJS editor into my Vue.js project, but encountered some difficulties. The editor was not visible in the browser, and the designated tag for the editor appeared empty. Here is my editor configuration: <template ...

Tips for aligning a button to the bottom of a card in Vuetify, regardless of the text length

I'm having trouble aligning buttons in my cards. I have a layout with 3 cards in a row, but the button's position changes when text is added to a specific card. I've tried passing different props and using various classes, but nothing seems ...