Vue.js v3 通过路由器链接传递数据

时间:2021-06-21 07:54:42

标签: vue.js vue-router

我在 Portfolio.vue 中有一个像这样的对象:

data() {
        return {
            portfolio: {
                front: [
                     { description: '1. project ', src: require("../assets/sample.jpg"),  slug: 'first'},
                     { description: '2. project', src: require("../assets/sample.jpg"),  slug: 'second' },
                     { description: '3. project', src: require("../assets/sample.jpg"),  slug: 'third' },
                ]
            }
        }
    }

Portfolio.vue:

<div class="">
               <div  v-for="(data,index) in portfolio.front" :key="index"> 
            <router-link  :to="'/portfolio/'+data.slug">  <div class="element" :data-description="data.description">
                   <img :src="data.src " alt=""> 
               </div>          
            </router-link>
               </div>
 </div>

PortfolioProduct.vue

<template>
    <div>
        <p>I want to take data to here. In here, i have to reach the data like this: portfolio.front.description</p>
    </div>
</template>

<script>

export default {
    props: {

    },
}
</script>

我的路线:

const routes = [
  {
    path: "/portfolio",
    name: "Portfolio",
    component: Portfolio,
  },
  {
    path: "/portfolio/:id",
    name: "PortfolioProduct",
    component: PortfolioProduct,
    props: true,
  },
];

我想将数据从 Portfolio.vue 提取到 PortfolioProduct.vue ,我无法解决。我正在使用 vue js3 ,如果你帮助我,我会很高兴。谢谢

1 个答案:

答案 0 :(得分:0)

您可能可以使用模板文字,更改路由器链接将如下所示:

<router-link  :to="`/portfolio/${data.slug}`">  <div class="element" :data-description="data.description">