【发布时间】:2021-08-01 06:54:06
【问题描述】:
我想触发一个函数,只要按下同级组件中的按钮,GETs 来自组件中的 http 服务器的数据。
SignUpForm.vue有一个按钮触发customSubmit()
customSubmit(){
//POST to API
const user = {
method: "POST",
headers: { "Content-Type": "application/json"},
body: JSON.stringify({newUser: this.newUser})
};
fetch("http://localhost:3080/api/user", user)
.then(response => response.json())
.then(data => console.log(data));
this.$emit('refresh', true)
this.clearForm();
}
父组件如下所示:
<template>
<div>
<SignUpForm @refresh="triggerRefresh($event)" />
<!-- <Exp /> -->
<Datatable :myRefresh="myRefresh" />
</div>
</template>
<script>
import SignUpForm from "./components/SignUpForm.vue";
import Datatable from "./components/Datatable.vue";
import Exp from "./components/exp copy.vue";
export default {
name: "App",
components: { Datatable, SignUpForm, Exp },
data() {
return {
myRefresh: false,
};
},
methods: {
triggerRefresh(bool) {
this.myRefresh = bool;
console.log(this.myRefresh);
},
},
};
</script>
现在我想要兄弟组件 Datatable.vue
尽快从服务器到fetch 数据,因为this.$emit('refresh', true) 在SignUpForm.vue 中被触发
这是来自Datatable.vue的脚本
export default {
data() {
return {
//Liste aller User
userData: null,
//temporärer User für das Details-Feld
printUser: [{ name: "", email: "", number: "" }],
//Property für den "read-Button"
showDetails: false,
//Property für den "Update-Button"
readOnly: true,
};
},
props: ["myRefresh"],
methods: {
pushFunction() {
fetch("http://localhost:3080/api/users")
.then((res) => res.json())
.then((data) => (this.userData = data));
},
readData(k) {
this.printUser.length = 0;
this.showDetails = true;
this.printUser.push(this.userData[k]);
},
editData(rowUser) {
if (!rowUser.readOnly) {
rowUser.readOnly = true;
const user = {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userData: this.userData }),
};
fetch("http://localhost:3080/api/users/patch", user)
.then((response) => response.json())
.then((data) => console.log(data));
} else {
rowUser.readOnly = false;
}
},
deleteData(k) {
fetch("http://localhost:3080/api/users/" + k, { method: "DELETE" }).catch(
(err) => console.log(err)
);
this.pushFunction();
},
//blaue Reihen
toggleHighlight(rowUser) {
if (rowUser.readOnly === false) {
return;
}
rowUser.isHighlight = !rowUser.isHighlight;
},
scrollDown() {
window.scrollTo(0, document.body.scrollHeight);
},
},
mounted() {
fetch("http://localhost:3080/api/users")
.then((res) => res.json())
.then((data) => (this.userData = data));
},
};
我真的希望有人可以帮助新手!
【问题讨论】:
-
您可以将事件推送到父级,然后更新父级中的模型值,然后将其绑定到另一个组件。您通过父级执行此类操作的次数越多,您的应用程序(并绑定在一起)就越复杂。因此,您通常会为这样的事情使用外部状态,您可以在其中推送值并在另一个组件中自动检索它(通过计算值)。在 Vue2 中,您通常会选择 Vuex,但在 Vue3 中引入了一种非常好的新方法:组合 API,它可以让您外包本地状态。
-
我会看看组合 API,谢谢!您是否也有针对我的代码的特定解决方案?
-
在 Datatable 中为
myRefresh设置一个观察者,并且不要为该值传递布尔值,而是传递一个生成的值(例如Math.random())。否则,您需要先发出 true,然后等待一个滴答声,然后将其重置为 false。如果值为真,您将需要检查 - 在观察者中。通过设置一个随机值,当值发生变化时,watch 处理程序就会被执行。如果要完全重新加载组件(不仅是重新获取数据),还可以使用组件的:key=""属性。一旦这个值改变,组件就会重新渲染。 -
你没有使用 VueJS3...利用
setup...
标签: vue.js events fetch vue-component vuejs3