简体   繁体   English

Vue.js如何使用Vue-Router将道具/参数从父组件传递到子组件

[英]Vue.js how to pass props/params from parent to child component with vue-router

I'm working on a simple app that shows a table of Exercises in a Dashboard. 我正在开发一个简单的应用程序,该应用程序在仪表板上显示一个练习表。 From there, a user can click on an ExerciseItem and open up an Exercise component. 用户可以从此处单击ExerciseItem,然后打开Exercise组件。 I've got the Dashboard component working, but cannot data to pass to the child Exercise component. 我已经使Dashboard组件正常工作,但是无法将数据传递给子Exercise组件。

Here is my Dashboard Code: 这是我的仪表板代码:

<template>
 <div class="">
   <NavDash />
    <div class="container pb-5 pt-5">
        <div class="row">
            <div class="col d-flex align-items-center">
            <h3>Dashboard</h3>
            <p class="ml-auto">{{ $store.state.user.email }}</p>
        </div>
    </div>

  <hr>
  <AddExercise />
  <hr>
  <table class="table">
  <tbody>
    <ExerciseItem
  v-for="(exercise_item, index) in this.$store.state.exercises"
  :exercise="exercise_item"
  key="index"
/>
   </tbody>
 </table>
  </div>
</div>
</template>

<script>
import { firebaseApp, exercisesRef, usersRef } from '../firebaseApp'
import AddExercise from './AddExercise.vue'
import ExerciseItem from './ExerciseItem.vue'
import NavDash from './NavDash.vue'

export default {
  methods: {
    signOut() {
    this.$store.dispatch('signOut')
   firebaseApp.auth().signOut()
 }
},
props: ['exercise'],
components: {
   AddExercise,
    ExerciseItem,
  NavDash
},
mounted() {
exercisesRef.on('value', snap => {
    let exercises = []
    snap.forEach(exercise => {
        exercises.push(exercise.val())
    })
    console.log('exercises', exercises)
    this.$store.dispatch('setExercises', exercises)
  })
 }
}
 </script>

And ExerciseItem.vue: 和ExerciseItem.vue:

<template>
 <tr>

<td class="text-left">
  <h4>{{exercise.title}}</h4>
  <p>{{exercise.description}}</p>
</td>
<td>
  {{exercise.categories}}
</td>
<td>
  <button @click="removeExercise(exercise)">X</button>
</td>
<td>
  <router-link to="/exercises/:id">LINK</router-link>
</td>
</tr>
</template>

<script>
 export default {
  props: ['exercise'],
}
</script>

Exercise.vue: Exercise.vue:

<template>
<div>
<NavDash />
Exercise {{ id }}
<h4>{{title}}</h4>
   <p>{{description}}</p>
</div> 
</template>

<script>
import { firebaseApp, exercisesRef, usersRef } from '../firebaseApp'
import NavDash from './NavDash.vue'

export default {
 props: ['id'],
 components: {
  NavDash
 }
}
</script>

And the router path: 和路由器路径:

{ path: '/exercises/:id', 
    name: 'Exercise',
    component: Exercise,
    props: {
        default: true
    }
}

Here is my Index.js inside of my Store directory: 这是我的Store目录中的Index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import { mutations } from './mutations'
import * as actions from './actions'
import * as getters from './getters'

Vue.use(Vuex)

const state = {
user: {},
exercises: {},
articles: {}
}

export default new Vuex.Store({
state,
 mutations,
actions,
getters
})

I tried following the documentation here: https://router.vuejs.org/en/essentials/passing-props.html 我尝试按照此处的文档进行操作: https : //router.vuejs.org/en/essentials/passing-props.html

but I can't seem to get the router-link to work properly. 但我似乎无法使路由器链接正常工作。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thank you! 谢谢!

Based on your route, the Exercise component receives an id prop, not an exercise prop. 根据您的路线,“锻炼”组件将接收一个id道具,而不是一个exercise道具。 I do not think you can pass objects in vue-router through routes, and even if you could, I think it would be a bad practice because it would have to serialize the object in the route. 我不认为您可以通过路由在vue-router中传递对象,即使您可以,我认为这也是一个坏习惯,因为它必须在路由中对对象进行序列化。

The Exercise component receives an id prop, and should use that to lookup the exercise. Exercise组件会收到一个id道具,并应使用它来查找锻炼。 Either through an ajax request, vuex, or some other source of state. 通过ajax请求,vuex或其他某种状态源。 Since you are using vuex, you could create a getter that returns an object where the keys are the exercise ids, and the values are the exercises. 由于您正在使用vuex,因此可以创建一个getter来返回一个对象,其中键是练习ID,值是练习。 Then you could lookup the exercise in the Exercise component using the id. 然后,您可以使用id在“锻炼”组件中查找锻炼。

Vuex getter Vuex吸气剂

exercisesById(state) {
    var obj = {};
    state.exercises.forEach(x => {
        obj[x.id] = x;
    });
    return obj;
}

Exercise component 运动成分

<script>
import { firebaseApp, exercisesRef, usersRef } from '../firebaseApp'
import NavDash from './NavDash.vue'

export default {
 props: ['id'],
 components: {
  NavDash
 },
 computed: {
     exercise() {
         return this.$store.getters.exercisesById(this.id);
     }
 },
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM