Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs variable in html attribute

Tags:

html

vue.js

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>
like image 805
jrock2004 Avatar asked Dec 22 '16 18:12

jrock2004


1 Answers

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.

like image 189
Belmin Bedak Avatar answered Oct 08 '22 18:10

Belmin Bedak