3

I am trying to use v-attr to add an attribute that contains a colon which is a delimiter for Vuejs key:value. It doesn't compile.

<svg>
    <use xmlns:xlink="http://www.w3.org/1999/xlink" 
        v-attr="xmlns:href: '#' + iconID"></use>
</svg>

I have also tried

<svg class="lego-icon">
    <use xmlns:xlink="http://www.w3.org/1999/xlink" 
        xmlns:href="#[[ iconID ]]"></use>
</svg>

Which has similar results.

4 Answers 4

1

You need to wrap your xlink:href in quotes so that Vue doesn't treat the colon as an argument separator (https://github.com/yyx990803/vue/issues/648)

<use v-attr="'xlink:href' : '#' + iconID">
Sign up to request clarification or add additional context in comments.

1 Comment

This is the valid answer
0

I haven't used vue.js, but looking at the source for v-attr, it appears as if it is designed to handle xlink:href.

The trouble is your attribute is wrong. It should be xlink:href, not xmlns:href. Try:

<svg>
    <use v-attr="xlink:href: '#' + iconID"></use>
</svg>

(If you are embedding SVG in an HTML page, you shouldn't need the xmlns:xlink="http://www.w3.org/1999/xlink").

1 Comment

I have the same problem and unfortunately, I couldn't get this to work: I always get this error: [Vue warn]: Invalid expression. Generated function body: scope.href:'/svg/ico.svg#'+scope.option.icon
0

With the new version of vue.js, you can use

<use v-bind:href="'#icon-' + iconId"></use>

or even

<use :href="'#icon-' + iconId"></use>

Vue.js seems to automatically set the attribute with the right namespace.

Comments

0
<template>
    <div v-bind:[attrName]="attrValue"></div>
</template>

<script>
    export default {
        data() {
            return {
                attrName : 'xlink:href',
                attrValue : '...',
            };
        }
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.