我的数组中有这种对象:
{
name: 'name1',
url: 'http://url-1.tld'
},
{
name: 'name2',
url: 'http://url-2.tld'
}
在 div 单击时,我想要将 window.location.href 指向 url,但我似乎无法从数据中获取 url 到我的方法。
<div v-for="person in persons" v-on:click="select($event)"></div>
select: function(event) {
window.location.href( ??? )
}
有人有建议吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
接受的答案有效,但会刷新页面。在 SPA 框架中,我们尽量避免刷新页面,因此正确答案是:
Vue:
this.$router.push(person.url)Nuxt:
this.$router.push({ name: 'routename' })您需要将
person作为参数传递给select,而不是$event:select: function(person) { window.location.href = person.url; }