【问题标题】:Vue.js v3 passing data through router linkVue.js v3 通过路由器链接传递数据
【发布时间】:2021-06-21 07:54:42
【问题描述】:

我在 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,如果你帮助我,我会很高兴。谢谢

【问题讨论】:

  • 你想获取哪些数据,只是蛞蝓还是整个对象?
  • 对象。不仅仅是蛞蝓。例如在我的对象中,如果 slug 是 'first' ,我必须达到 { description: '1. project ', src: require("../assets/sample.jpg"), slug: 'first'},全部。

标签: vue.js vue-router


【解决方案1】:

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

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

【讨论】:

  • 我的结构没有错误。我必须将对象从一个组件到达另一个组件。我的结构正常,没有语法错误
猜你喜欢
  • 2018-04-03
  • 2021-06-27
  • 2023-03-31
  • 2017-04-23
  • 2023-01-20
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多