0

I created 2 Vue components:

Vue.component('vimeo', {
    template: '<iframe src="https://player.vimeo.com/video/' + this.videoId + '" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>',
    props: ['videoId']
});

Vue.component('videos', {
    template: '<div id="videos">' + 
                '<div v-for="videoId in videoIds">' +
                    '<vimeo :video-id="videoId"></vimeo>' +
                '</div>' +
              '</div>',
    computed: {
        videoIds: function() {
            return store.state.videoIds;
        }
    }
});

However, the this.videoId is always undefined in the 'vimeo' component template. If I change the template to:

<h1>{{ videoId }}</h1>

the prop value can be displayed properly.

I have searched the internet for solutions to this, but unfortunately, I have not got any.

1 Answer 1

1

The code you have is basically trying to use this.videoId at the time of the template definition, before any components have been initialized.

Instead what you'll have to do is bind the videoId value so that it is translated at render time. I would do this with a computed property since you need to concatenate the vimeo URL prefix.

Something like:

computed: {
  videoUrl() { return 'https://player.vimeo.com/video/' + this.videoId; }
},
template: '<iframe :src="videoUrl" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'

Note the colon in :src for the dynamic binding. See the docs for more info on this.

Sign up to request clarification or add additional context in comments.

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.