我怎样才能通过在原子设计模式中发射来从 vuetify select 获得改变的价值?
How could I get changed value from vuetify select by emitting in atomic design pattern?
我正在尝试使用 $emit
获取 vuetify v-select
的更改值,但它不起作用。
我通过应用原子设计模式(原子(子组件,不与商店连接)、有机体(父组件))和 vuex 商店来划分组件。
我认为 $emit 数据没问题,但在这个过程之后一切都不起作用。
这是用于管理页面的新应用程序
使用 vue, vuex, vuetify,
原子设计连接到 API 服务器。
组件
子组件 - 在 atoms 文件夹中
<template>
<v-select
:items="list"
:label="label"
v-model="selected"
item-value="id"
item-text="name"
return-object
@change="changeSelected"
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
data() {
return {
selected: this.defaultSelected
};
},
methods: {
changeSelected(newValue) {
console.log(newValue); // display changed new data
this.$emit("changeSelected", newValue);
}
}
};
</script>
父组件 - 在生物文件夹中
<template>
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
@change-selected="changeSelected" // problem issue?
>
</v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected() {
console.log("changeSelected"); // doesn't work
this.$store.dispatch("setSelected", { payload: this.selected });
}
}
};
</script>
vuex 商店
index.js
export default new Vuex.Store({
modules: {
xxx
},
state: {
list: [
{
name: "aaaaa",
id: "001"
},
{
name: "bbbbb",
id: "002"
}
]
},
getters: {},
mutations: {},
actions: {}
});
xxx.js
export default {
selected: { id: "001" }
},
getters: {
//
},
mutations: {
updateSelected(state, payload) {
console.log("payload"); // doesn't work
console.log(payload);
state.selected = payload;
console.log(state.selected);
}
},
actions: {
setSelected({ commit }, payload) {
console.log("Action"); // doesn't work
commit("updateSelected", payload);
}
}
};
在changeSelected
函数后不打印任何控制台日志。
Unlike components and props, event names don’t provide any automatic
case transformation. Instead, the name of an emitted event must
exactly match the name used to listen to that event.
这意味着如果你发出像 $emit('changeSelected')
这样的事件,那么你需要使用 @changeSelected
。 @change-selected
将不起作用。
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
@changeSelected="changeSelected"
>
</v-select-child>
我在下面找到了解决方案:
子组件
<template>
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
return-object
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
computed: {
selected: {
get() {
return this.defaultSelected;
},
set(newVal) {
if (this.selected !== newVal) {
this.$emit("changeSelected", newVal);
}
}
}
}
};
</script>
父组件
<template>
<v-select-child
:label="label"
:list="list"
:defaultSelected="selected"
@changeSelected="changeSelected" // fix the property using camelCase
></v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected(val) { // val: changed object value
this.$store.dispatch("setSelected", { id:val.id });
}
}
};
</script>
你也可以用watch;
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
></v-select>
</template>
...
watch:{
selected(){
this.$emit('changeValue', this.selected.id');
}
}
...
来自 parent;
<child @changeValue="id = $event" .. />
我正在尝试使用 $emit
获取 vuetify v-select
的更改值,但它不起作用。
我通过应用原子设计模式(原子(子组件,不与商店连接)、有机体(父组件))和 vuex 商店来划分组件。
我认为 $emit 数据没问题,但在这个过程之后一切都不起作用。
这是用于管理页面的新应用程序
使用 vue, vuex, vuetify,
原子设计连接到 API 服务器。
组件
子组件 - 在 atoms 文件夹中
<template>
<v-select
:items="list"
:label="label"
v-model="selected"
item-value="id"
item-text="name"
return-object
@change="changeSelected"
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
data() {
return {
selected: this.defaultSelected
};
},
methods: {
changeSelected(newValue) {
console.log(newValue); // display changed new data
this.$emit("changeSelected", newValue);
}
}
};
</script>
父组件 - 在生物文件夹中
<template>
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
@change-selected="changeSelected" // problem issue?
>
</v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected() {
console.log("changeSelected"); // doesn't work
this.$store.dispatch("setSelected", { payload: this.selected });
}
}
};
</script>
vuex 商店
index.js
export default new Vuex.Store({
modules: {
xxx
},
state: {
list: [
{
name: "aaaaa",
id: "001"
},
{
name: "bbbbb",
id: "002"
}
]
},
getters: {},
mutations: {},
actions: {}
});
xxx.js
export default {
selected: { id: "001" }
},
getters: {
//
},
mutations: {
updateSelected(state, payload) {
console.log("payload"); // doesn't work
console.log(payload);
state.selected = payload;
console.log(state.selected);
}
},
actions: {
setSelected({ commit }, payload) {
console.log("Action"); // doesn't work
commit("updateSelected", payload);
}
}
};
在changeSelected
函数后不打印任何控制台日志。
Unlike components and props, event names don’t provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event.
这意味着如果你发出像 $emit('changeSelected')
这样的事件,那么你需要使用 @changeSelected
。 @change-selected
将不起作用。
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
@changeSelected="changeSelected"
>
</v-select-child>
我在下面找到了解决方案:
子组件
<template>
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
return-object
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
computed: {
selected: {
get() {
return this.defaultSelected;
},
set(newVal) {
if (this.selected !== newVal) {
this.$emit("changeSelected", newVal);
}
}
}
}
};
</script>
父组件
<template>
<v-select-child
:label="label"
:list="list"
:defaultSelected="selected"
@changeSelected="changeSelected" // fix the property using camelCase
></v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected(val) { // val: changed object value
this.$store.dispatch("setSelected", { id:val.id });
}
}
};
</script>
你也可以用watch;
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
></v-select>
</template>
...
watch:{
selected(){
this.$emit('changeValue', this.selected.id');
}
}
...
来自 parent;
<child @changeValue="id = $event" .. />