Javascript 如何在VueJS中传递道具?

Javascript 如何在VueJS中传递道具?,javascript,vue.js,vuex,vuetify.js,Javascript,Vue.js,Vuex,Vuetify.js,我的应用程序中有一个简单的设置,由2个组件组成。HelloWorld组件,然后是对话框组件。对话框组件接受从HelloWorld组件传递的道具。HelloWorld中的数据是通过在数组上循环来呈现的。我试图实现的是,当我单击其中一个元素时,我希望对话框中预先填充我单击的元素的名称和年龄。我不知道我怎么能做到 检查这个完整的设置 这是我的Vuex商店:- state: { userData: [ { name: "Sam", age: "24"

我的应用程序中有一个简单的设置,由2个组件组成。HelloWorld组件,然后是对话框组件。对话框组件接受从HelloWorld组件传递的道具。HelloWorld中的数据是通过在数组上循环来呈现的。我试图实现的是,当我单击其中一个元素时,我希望对话框中预先填充我单击的元素的名称和年龄。我不知道我怎么能做到

检查这个完整的设置

这是我的Vuex商店:-

state: {
    userData: [
      {
        name: "Sam",
        age: "24"
      },
      {
        name: "Max",
        age: "28"
      }
    ],
    dialog: false
  },
  getters: {
    getUserData: state => state.userData,
    getDialog: state => state.dialog
  },
  mutations: {
    openDialog(state) {
      state.dialog = true;
    }
  }
这是我的HelloWorld组件:-

<template>
  <v-container>
    <v-layout justify-center>
      <v-card>
        <v-card-text>
          <div v-for="user in getUserData" :key="user.age">
            <span>{{user.name}}</span>
            <span>{{user.age}}</span>
            <v-icon @click="openDialog">create</v-icon>
          </div>
        </v-card-text>
      </v-card>
    </v-layout>
    <Dialog/>
  </v-container>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";
import Dialog from "./Dialog";
export default {
  name: "HelloWorld",
  components: {
    Dialog
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      getUserData: "getUserData"
    })
  },
  methods: {
    ...mapMutations({
      openDialog: "openDialog"
    })
  }
};
</script>
<template>
  <v-dialog v-model="getDialog" max-width="300px">
    <v-card>
      <v-card-text>
        <v-text-field v-model="title"></v-text-field>
        <div class="mt-5">{{age}}</div>
      </v-card-text>
    </v-card>
  </v-dialog>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: {
    title: {
      type: String
    },
    age: {
      type: Number
    }
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      getDialog: "getDialog"
    })
  }
};
</script>
这是我的对话框组件:-

<template>
  <v-container>
    <v-layout justify-center>
      <v-card>
        <v-card-text>
          <div v-for="user in getUserData" :key="user.age">
            <span>{{user.name}}</span>
            <span>{{user.age}}</span>
            <v-icon @click="openDialog">create</v-icon>
          </div>
        </v-card-text>
      </v-card>
    </v-layout>
    <Dialog/>
  </v-container>
</template>

<script>
import { mapGetters, mapMutations } from "vuex";
import Dialog from "./Dialog";
export default {
  name: "HelloWorld",
  components: {
    Dialog
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      getUserData: "getUserData"
    })
  },
  methods: {
    ...mapMutations({
      openDialog: "openDialog"
    })
  }
};
</script>
<template>
  <v-dialog v-model="getDialog" max-width="300px">
    <v-card>
      <v-card-text>
        <v-text-field v-model="title"></v-text-field>
        <div class="mt-5">{{age}}</div>
      </v-card-text>
    </v-card>
  </v-dialog>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: {
    title: {
      type: String
    },
    age: {
      type: Number
    }
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      getDialog: "getDialog"
    })
  }
};
</script>

谢谢你的帮助。谢谢。

别忘了在数据中定义currentuserName和currentuserAge,否则它们不会被删除reactive@Phil我写了一篇关于在数据中定义currentuserName和currentuserAge的评论。我应该明确定义我的数据属性吗?@something不管怎样,如果这个问题解决了,或者你需要其他帮助,请告诉我。谢谢大家。轻松优雅。我一直想知道怎么做,但方法很简单。你们是摇滚明星D
<div v-for="(user, index) in getUserData" :key="user.age">
        <span>{{user.name}}</span>
        <span>{{user.age}}</span>
        <v-icon @click="openDialogAndGetCurrentUser(index)">create</v-icon>
 </div>
openDialogAndGetCurrentUser(index) {
   this.openDialog();
   /* define these in your data to be passed to child */
   this.currentuserName = this.getUserData[index].name;
   this.currentuserAge = this.getUserData[index].age;
}
<Dialog 
  :title="currentuserName"
  :age="currentuserAge"/>