gpt4 book ai didi

javascript - Vue js在组件之间发送数据

转载 作者:行者123 更新时间:2023-11-30 21:11:20 25 4
gpt4 key购买 nike

我想知道如何在两个组件之间发送数据。所以我想将在 selectedBase.price 上呈现的动态值发送到另一个组件,并在另一个组件上呈现。我试过 Prop 但没有成功。

<v-layout row wrap primary-title v-for="base in bases" :key="base.id">
<v-layout column>
<v-flex xs6 offset-xs3>
<v-avatar size="80px" class="grey lighten-1">
<img :src="base.href" :class="{selected: selectedBase.id == base.id}" @click="selectedBase = base" alt="avatar">
</v-avatar>
</v-flex>
</v-layout>
<v-layout column>
<v-flex xs6 offset-xs4>
<v-subheader>{{base.name}} {{base.price}}€ {{selectedBase.price}}</v-subheader>
</v-flex>
</v-layout>
</v-layout>

<script>

export default {

data() {
return {
selectedBase: {},
bases: [{
id: 1,
name: "black",
price: 4,
href: "../../static/black.jpg"
}, {
id: 2,
name: "white",
price: 6,
href: "../../static/white.jpg"
}]
}
},
computed: {

totalBase: function() {
var totalBase = 0;
for (var i = 0; i < this.bases.length; i++) {
if (this.bases[i].selected) {
totalBase += this.bases[i].price;
}
}
return totalBase;
}
},
methods: {
getSelectedBase() {
return this.selectedBase;
}
}
}

</script>

最佳答案

是的,props 是正确的方式。

ParentComponent.vue

<template>
<your-component your-prop="123"></your-component>
</template>

<script>
import YourComponent from '/path/to/YourComponent'

export default {
data() {
return { }
},
components { YourComponent }
}
</script>

ChildComponent.vue

<template>
<div>My prop is: {{ yourProp }}</div>
</template>

<script>
export default {
data() {
return { }
},
props: ['your-prop']
}
</script>

您也可以使用 Vuex 在应用之间共享状态,但我认为情况并非如此。

关于javascript - Vue js在组件之间发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46100886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com