为什么排序方法在父组件中不起作用?
这是我在Vuejs中的第一个项目。 我有一个产品列表,想按价格进行排序。我构建了两个组件,并试图通过发射事件将类别方法从子组件(下拉按钮)传递给父母。但是经过大量尝试,我找不到任何帮助!
这个孩子组成部分:
<template>
<div class="dropdown">
<button
@click="toggleShow(); $emit('sortPrice')"
class="dropbtn"
>
{{ title }}
<span class="material-icons-outlined"> {{ icon }} </span>
</button>
<div v-if="showMenu" class="menu">
<div class="menu-item" v-for="(item, index) in this.items" :key="index">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
name: "Dropdown-menu",
props: {
title: String,
icon: String,
items: {
type: Object,
required: true,
},
},
data() {
return {
showMenu: false
};
},
methods: {
toggleShow: function () {
this.showMenu = !this.showMenu;
},
sortPrice: function () {
this.$emit("sort", this.sortPrice);
},
},
};
</script>
此父级组件:
<template>
<dropdown
:title="sortedBy"
:items="arrangements"
:icon="material_icons"
@sort="sortByPrice"
></dropdown>
</template>
<script>
import Dropdown from "@/components/Dropdown.vue";
export default {
components: {
Dropdown,
},
data() {
return {
sortedBy: "Featured",
arrangements: ["Featured", "Lowest", "Highest"],
material_icons: "expand_more",
productData: require("@/data/store-data.json"),
};
},
methods: {
sortByPrice: function () {
let realProducts = this.productData.products;
let sortedProducts = realProducts.sort((a, b) => {
if (this.sortedBy === "Highest") {
return b.price - a.price;
} else if (this.sortedBy === "Lowest") {
return a.price - b.price;
}
});
return sortedProducts;
},
},
};
</script>
This is the first project for me in VueJS.
I have a product list and want to sort it by price. I built two components and tried to pass a sort method to the parent from the child component (dropdown button) by emitting an event. but after a lot of attempts, I can't find the wrong with my code, any help!
This Child Component:
<template>
<div class="dropdown">
<button
@click="toggleShow(); $emit('sortPrice')"
class="dropbtn"
>
{{ title }}
<span class="material-icons-outlined"> {{ icon }} </span>
</button>
<div v-if="showMenu" class="menu">
<div class="menu-item" v-for="(item, index) in this.items" :key="index">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
name: "Dropdown-menu",
props: {
title: String,
icon: String,
items: {
type: Object,
required: true,
},
},
data() {
return {
showMenu: false
};
},
methods: {
toggleShow: function () {
this.showMenu = !this.showMenu;
},
sortPrice: function () {
this.$emit("sort", this.sortPrice);
},
},
};
</script>
This Parent Component:
<template>
<dropdown
:title="sortedBy"
:items="arrangements"
:icon="material_icons"
@sort="sortByPrice"
></dropdown>
</template>
<script>
import Dropdown from "@/components/Dropdown.vue";
export default {
components: {
Dropdown,
},
data() {
return {
sortedBy: "Featured",
arrangements: ["Featured", "Lowest", "Highest"],
material_icons: "expand_more",
productData: require("@/data/store-data.json"),
};
},
methods: {
sortByPrice: function () {
let realProducts = this.productData.products;
let sortedProducts = realProducts.sort((a, b) => {
if (this.sortedBy === "Highest") {
return b.price - a.price;
} else if (this.sortedBy === "Lowest") {
return a.price - b.price;
}
});
return sortedProducts;
},
},
};
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议:
@conce@click =“ sortprice(item)”
在sortprice函数中调用菜单界div的sortprice函数函数(item){
并将其作为第二个参数传递给您的emit调用:this。$ emit(“ stort”,item);
。父必须知道sortByPrice:function(item){
并使用它来设置sortedby属性:this.sortedby = item;
sortedProducts
。例如,父母:
和孩子下拉。
Suggestions:
@click="sortPrice(item)"
function (item) {
and pass it as a second parameter to your emit call:this.$emit("sort", item);
. The parent must know what was selectedsortByPrice: function (item) {
and use it to set the sortedBy property:this.sortedBy = item;
sortedProducts
.For example, the parent:
and the child Dropdown.vue component: