When the div is clicked, jump to the Vue method of the url defined in data
P粉426780515
P粉426780515 2023-10-23 15:46:15
0
2
592

I have this kind of object in my array:

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

When the div is clicked, I want to point the window.location.href to the url, but I can't seem to get the url from the data into my method.

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

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

Does anyone have any suggestions?

P粉426780515
P粉426780515

reply all(2)
P粉399090746

The accepted answer works but will refresh the page. In the SPA framework, we try to avoid refreshing the page, so the correct answer is:

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

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

P粉086993788

You need to pass person as a parameter to select, not $event:

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

select: function(person) {
  window.location.href = person.url;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template