我有以下代码
<body>
<div class="content" id="app">
<file-management></file-management>
<attachment-list></attachment-list>
</div>
<script src="{{ asset('js/app.js') }}"></script>
</body>
FileManagement组件代码:
<template>
<div>
<button type="button" @click="storeList()">
Save
</button>
</div>
</template>
<script>
export default {
methods: {
storeList: function () {
axios.post('/list', this.data, config)
.then(() => {
// on save I want to be able to load the table again that is found in AttachmentList component
});
},
}
}
</script>
AttachmentList组件代码:
<template>
<div>
<tr v-for="attachment in attachments" :key="attachment.id">
<td>{{ attachment.name }}</td>
</tr>
</div>
</template>
<script>
export default {
data() {
return {
attachments: []
}
},
methods: {
getList() {
axios.get(`/list`)
.then((data) => {
this.attachments = data;
});
}
}
}
</script>
我想要做的是,当我单击“另一个组件中的保存”(在post请求完成后)时,我希望能够加载列表的表。我怎样才能做到这一点呢?
发布于 2020-05-08 05:49:51
最简单的方法是让您的FileManagement
组件发出父级可以侦听的事件,然后触发AttachmentList#getList()
方法。
例如
// in AttachmentList
methods: {
async storeList () {
await axios.post('/list', this.data, config)
this.$emit('list-updated')
}
}
在父模板中
<file-management @list-updated="$refs.list.getList()"></file-management>
<attachment-list ref="list"></attachment-list>
发布于 2020-05-08 05:49:34
我就是这样做的。
FileManagement
发出信号。AttachmentList
组件作为支柱。H 212H 113
在v-if
中使用此标志显示/隐藏表。H 215F 216
https://stackoverflow.com/questions/61672744
复制相似问题