4

I have this kind of objects in an array:

{
  name: 'name1',
  url: 'http://url-1.tld'
},
{
  name: 'name2',
  url: 'http://url-2.tld'
}

On div click, I want to to a window.location.href to the url, but I can't seem to get the url from the data to my method.

<div v-for="person in persons" v-on:click="select($event)"></div>

select: function(event) {
  window.location.href( ??? )
}

Anybody have suggestions?

2 Answers 2

10

You need to pass the person as the argument to select, not $event:

<div v-for="person in persons" v-on:click="select(person)"></div>

select: function(person) {
  window.location.href = person.url;
}
Sign up to request clarification or add additional context in comments.

3 Comments

window.location.href is not a function.
@ill In the original answer, window.location.href was used as a function (eg. window.location.href(xxx).
2

The accepted answer works but it refreshes the page. In SPA frameworks we try to avoid refreshing the page so the correct answer is:

Vue: this.$router.push(person.url)

Nuxt: this.$router.push({ name: 'routename' })

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.