英文:
How can I pass an object in params when using $router.push()?
问题
以下是您要翻译的内容:
"我想循环遍历一个对象数组,并且当我点击一个项目时,我想显示一个页面,该页面填充了来自其他组件的数据。
这是我的 router.js
{
path: '/artists',
component: () => import('../views/Artists/ArtistsStart.vue'),
children: [
{
path: '',
component: () => import('../views/Artists/Artists.vue')
},
{
path: ':name',
component: () => import('../views/Artists/Artist.vue')
}
]
}
ArtistsStart.vue
只有 <router-view>
和一个过渡动画,所以我认为它不重要
Artists.vue
<template>
<div v-for="(artist, index) in artists" :key="index" @click="artistLink(artist)">
{{artist.name}}
</div>
</template>
<script>
export default {
data() {
return {
artists: [{
name: 'artist 1',
route: 'artist1',
text: 'lorem ipsum',
imageUrl: ''
},
{
name: 'artist 2',
route: 'artist2',
text: 'lorem ipsum',
imageUrl: ''
}
]
}
},
methods: {
artistLink(artist) {
this.$router.push(`/artists/${artist.route}`);
}
}
}
</script>
我没有使用 <router-link>
因为我不想通过 URL 传递文本和其他数据
Artist.vue
<template>
<div>
<div>
{{name}}
</div>
<div>
{{text}}
</div>
</div>
</template>
<script>
export default {
data() {
return {
name: this.$route.params.name,
text: this.$route.params.text
}
}
}
</script>
我只能在 Artist.vue
页面中显示艺术家的名字。"
英文:
I want to loop through an array of objects and when I click on an item I want to display a page populated with data from the other component.
Here is my router.js
{
path: '/artists',
component: () => import('../views/Artists/ArtistsStart.vue'),
children: [
{
path: '',
component: () => import('../views/Artists/Artists.vue')
},
{
path: ':name',
component: () => import('../views/Artists/Artist.vue')
}
]
}
ArtistsStart.vue
only has <router-view>
and a transition animation so I don't think it's important
Artists.vue
<template>
<div v-for="(artist, index) in artists" :key="index" @click="artistLink(artist)">
{{artist.name}}
</div>
</template>
<script>
export default {
data() {
return {
artists: [{
name: 'artist 1',
route: 'artist1',
text: 'lorem ipsum',
imageUrl: ''
},
{
name: 'artist 2',
route: 'artist2',
text: 'lorem ipsum',
imageUrl: ''
}
]
}
},
methods: {
artistLink(artist) {
this.$router.push(`/artists/${artist.route}`);
}
}
}
</script>
I didn't use <router-link>
because I don't want to pass the text and other data through the URL
Artist.vue
<template>
<div>
<div>
{{name}}
</div>
<div>
{{text}}
</div>
</div>
</template>
<script>
export default {
data() {
return {
name: this.$route.params.name,
text: this.$route.params.text
}
}
}
</script>
I can only display the artist name in the Artist.vue
page.
答案1
得分: 1
router.push
和router-link
的目的都是执行相同的操作,即将您重定向到一个新的位置/地址,例如/artists/1
(它们只是以不同的方式执行,动态与静态)。因此,原则上,该组件负责查找与此地址相关的数据,而不是请求者。在您的情况下,Artist.vue
的责任是查找具有给定艺术家ID或艺术家路由的数据。实际上,您应该在此组件中有一些方法,可以从本地数据存储中获取数据,例如使用vuex存储或纯粹的js(将artist
列表放入artist.js
中,并在Artists.vue
和Artist.vue
中重用它),或者从服务器使用ajax获取数据。
英文:
router.push
and router-link
are meant to do the same thing which is to redirect you to a new location/address, for example, /artists/1
(they are just doing it in a different way, dynamically vs statically). So in principle, it is the responsibility of that component to find the related data for this address, not the requester. In your case, it is the responsibility of the Artist.vue
to find the data with the given artist Id, or artist route. In reality, you should have some method in this component to either fetch data from a local data store, e.g. using a vuex store or just plain js (put the artist
list into a artist.js
and reuse that in both Artists.vue
, Artist.vue
), or from a server using ajax.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论