javascript - 如何在兄弟提交上呈现 vuejs 组件
问题描述
我有以下代码
<body>
<div class="content" id="app">
<file-management></file-management>
<attachment-list></attachment-list>
</div>
<script src="{{ asset('js/app.js') }}"></script>
</body>
文件管理组件代码:
<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>
附件列表组件代码:
<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>
我想要做的是,当我在另一个组件中单击保存时(在发布请求完成后),我希望能够加载列表的表格。我将如何实现这一目标?
解决方案
最简单的方法是让您的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>
推荐阅读
- server - 什么是解释水平sclaing的正确方法?
- javascript - 在服务器上反应渲染 404 中的嵌套路由
- python - 从 CMD 打开 .py 文件和直接从文件资源管理器打开它有什么区别?
- javascript - JavaScript 中数字的阶乘引发 RangeError:超出最大调用堆栈大小
- laravel - Laravel Cashier - 订阅中的多种产品
- c# - 将 JSON 数据从一种结构转换为另一种结构的最佳方式
- angular - 我有角度组件的问题
- python - 如何在 PyQt5 的主窗口顶部显示一个新窗口
- sql - SQL Server 事务 - 如何使用它们
- python - Duplicate columns issue when applying migrations files by another team member in django