如何在使用 $router.push() 时传递参数对象?

huangapple go评论79阅读模式
英文:

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: &#39;/artists&#39;,
    component: () =&gt; import(&#39;../views/Artists/ArtistsStart.vue&#39;),
    children: [
      {
        path: &#39;&#39;,
        component: () =&gt; import(&#39;../views/Artists/Artists.vue&#39;)
      },
      {
        path: &#39;:name&#39;,
        component: () =&gt; import(&#39;../views/Artists/Artist.vue&#39;)
      }
    ]
  }

ArtistsStart.vue only has &lt;router-view&gt; and a transition animation so I don't think it's important

Artists.vue

&lt;template&gt;
    &lt;div v-for=&quot;(artist, index) in artists&quot; :key=&quot;index&quot; @click=&quot;artistLink(artist)&quot;&gt;
        {{artist.name}}
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
    export default {
        data() {
            return {
                artists: [{
                        name: &#39;artist 1&#39;,
                        route: &#39;artist1&#39;,
                        text: &#39;lorem ipsum&#39;,
                        imageUrl: &#39;&#39;
                    },
                    {
                        name: &#39;artist 2&#39;,
                        route: &#39;artist2&#39;,
                        text: &#39;lorem ipsum&#39;,
                        imageUrl: &#39;&#39;
                    }
                ]
            }
        },
        methods: {
            artistLink(artist) {
                this.$router.push(`/artists/${artist.route}`);
            }
        }
    }
&lt;/script&gt;

I didn't use &lt;router-link&gt; because I don't want to pass the text and other data through the URL

Artist.vue

&lt;template&gt;
    &lt;div&gt;
        &lt;div&gt;
            {{name}}
        &lt;/div&gt;
        &lt;div&gt;
            {{text}}
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
    export default {
        data() {
            return {
            name: this.$route.params.name,
            text: this.$route.params.text
            }
        }
    }
&lt;/script&gt;

I can only display the artist name in the Artist.vue page.

答案1

得分: 1

router.pushrouter-link的目的都是执行相同的操作,即将您重定向到一个新的位置/地址,例如/artists/1(它们只是以不同的方式执行,动态与静态)。因此,原则上,该组件负责查找与此地址相关的数据,而不是请求者。在您的情况下,Artist.vue的责任是查找具有给定艺术家ID或艺术家路由的数据。实际上,您应该在此组件中有一些方法,可以从本地数据存储中获取数据,例如使用vuex存储或纯粹的js(将artist列表放入artist.js中,并在Artists.vueArtist.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.

huangapple
  • 本文由 发表于 2020年1月6日 02:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定