繁体   English   中英

如何在兄弟提交上呈现 vuejs 组件

[英]How to render vuejs component on sibling submit

我有以下代码

<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>

这就是我将如何进行。

  • 为兄弟姐妹创建父组件。
  • 添加一个 boolean 数据成员(标志)到它与单击按钮的状态。
  • 单击按钮时从FileManagement发出信号。
  • 在父组件中捕获此信号以设置标志。
  • 将此标志作为道具传递给AttachmentList组件。
  • v-if中使用这个标志来显示/隐藏表格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM