Home  >  Q&A  >  body text

Setting props as part of href attribute in VueJS

I have defined an icon component and want to append props to the href link address as part of the URL. How can this be done correctly?

In fact, I want the number of attributes href to look like this:

href="./img/icon.svg#ico_copy"

Vue.component('icon', {
  template: `
  <svg class="iconz">
  <use v-bind:href="'./img/icon.svg#'+ props.label"></use>
  </svg>`,
  props: {
    label: String,
  },
});
  <icon label="ico_copy"></icon>

P粉653045807P粉653045807188 days ago364

reply all(1)I'll reply

  • P粉724737511

    P粉7247375112024-04-01 13:33:55

    You can create a computed property like this:

    computed: {
      url() {
        return `./img/icon.svg#${this.label}`;
      }
    }

    reply
    0
  • Cancelreply