jrock2004
jrock2004

Reputation: 3501

vuejs variable in html attribute

So I am trying to set the src of an element to a js variable and its just not working. I have tried a few ways and I cannot get it to work. Here is one way

<source src="{{ this.show.podcastUrl }}" type="audio/mpeg">

I also tried

<source v-bind:src="{{ this.show.podcastUrl }}" type="audio/mpeg">

And

<source :src="{{ this.show.podcastUrl }}" type="audio/mpeg">

What am I doing wrong? Here is my component

<template>
    <div class="panel panel-default">
        <div class="panel-heading">
            {{ this.show.name }}
            <div class="pull-right">
                {{ this.show.number }}
            </div>
        </div>
        <div class="panel-body">
            <ul>
                <li>Air Date: </li>
                <li>
                    <audio controls>
                        <source v-bind:src="{{ this.show.podcastUrl }}" type="audio/mpeg">
                    </audio>
                </li>
            </ul>
        </div>
    </div>
</template>


<script>
    export default {
        mounted() {
            console.log(this.show);
        },

        props: {
            show: {
                type: Object,
                required: true
            }
        }
    }
</script>

Upvotes: 30

Views: 54753

Answers (1)

Belmin Bedak
Belmin Bedak

Reputation: 9201

It's because you are using mustaches into v-bind directive - which is not allowed.

Mustaches {{}} into VueJS are related to templating, v-bind is passed to JS - so mustaches as part of template engine are not allowed into the v-bind directive.

This should be correct way:

<source :src="show.podcastUrl" type="audio/mpeg">

Also this is not needed here.

Upvotes: 68

Related Questions