我正在努力寻找一个合适的解决方案,它需要在 vuejs 中进行高级的父子通信。可以有许多不同的父组件,它们具有如何保存数据的逻辑。从另一侧来看,只有一个子组件,它具有元素列表和用于创建新元素的表单,但它不知道如何保存数据。
问题是:是否有任何其他方法(更好的方法)来具有相同的功能,但要摆脱 this.$refs.child
链接。例如,我想知道是否可以将函数(SaveParent1(...)
或 SaveParent2(...)
)传递给子组件。但问题是该函数包含一些父级变量,这些变量在子上下文中不可用,并且这些变量可能在运行时更改。
简单说明一下:
- 现实生活中的
SaveParent1
和SaveParent2
方法返回 promise (axios)。 子组件
就像使用的 CRUD 其他地方。
目前,通信看起来像这样:CHILD -event->
PARENT -ref->
CHILD。
以下是示例:
<div id="app">
<h2>😀Advanced Parent-Child Communication:</h2>
<parent-component1 param1="ABC"></parent-component1>
<parent-component2 param2="XYZ"></parent-component2>
</div>
Vue.component('parent-component1', {
props: { param1: { type: String, required: true } },
methods: {
onChildSubmit(p) {
// Here will be some logic to save the param. Many different parents might have different logic and all of them use the same child component. So child-component contains list, form and validation message but does not know how to save the param to the database.
var error = SaveParent1({ form: { p: p, param1: this.param1 } });
if (error)
this.$refs.child.paramFailed(error);
else
this.$refs.child.paramAdded(p);
}
},
template: `<div class="parent"><p>Here is parent ONE:</p><child-component ref="child" @submit="onChildSubmit"></child-component></div>`
});
Vue.component('parent-component2', {
props: { param2: { type: String, required: true } },
methods: {
onChildSubmit(p) {
// Here is a different logic to save the param. In prictice it is gonna be different requests to the server.
var error = SaveParent2({ form: { p: p, param2: this.param2 } });
if (error)
this.$refs.child.paramFailed(error);
else
this.$refs.child.paramAdded(p);
}
},
template: `<div class="parent"><p>Here is parent TWO:</p><child-component ref="child" @submit="onChildSubmit"></child-component></div>`
});
Vue.component('child-component', {
data() {
return {
currentParam: "",
allParams: [],
errorMessage: ""
}
},
methods: {
submit() {
this.errorMessage = "";
this.$emit('submit', this.currentParam);
},
paramAdded(p) {
this.currentParam = "";
this.allParams.push(p);
},
paramFailed(msg) {
this.errorMessage = msg;
}
},
template: `<div><ol><li v-for="p in allParams">{{p}}</li></ol><label>Add Param: <input v-model="currentParam"></label><button @click="submit" :disabled="!currentParam">Submit</button><p class="error">{{errorMessage}}</p></div>`
});
function SaveParent1(data) {
// Axios API to save data. Bellow is a simulation.
if (Math.random() > 0.5)
return null;
else
return 'Parent1: You are not lucky today';
}
function SaveParent2(data) {
// Axios API to save data. Bellow is a simulation.
if (Math.random() > 0.5)
return null;
else
return 'Parent2: You are not lucky today';
}
new Vue({
el: "#app"
});
最佳答案
从架构上来说,我建议拥有一个完全从组件层次结构中抽象出来的服务,并且您可以在每个组件中注入(inject)和使用该服务。使用这种组件层次结构和架构很容易遇到这些问题。从组件中抽象出尽可能多的功能和业务逻辑非常重要。我认为这些现代框架中的组件只不过是类固醇上的 HTML 模板,它们最多应该充当 Controller ,使它们尽可能愚蠢和精简,这样您就不会遇到这些情况。我不了解 vue.js
,所以我无法为您提供技术解决方案,但希望此指示有所帮助
关于javascript - Vuejs : Passing SAVE function into CRUD component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217605/