简体   繁体   English

点击后如何重新渲染Vue

[英]How to re-render after a click Vue

I have a table component that is rendered when a node on my diagram is selected.我有一个表格组件,当我的图表上的一个节点被选中时呈现。 In my template for the component that is rendering the table I have this:在呈现表格的组件的模板中,我有这个:

<div v-if="this.nodeName != ''" class="flex">
    <table-details :title="tableName" :cardListData="valuesCardData"/>
  </div>

the problem is once the name is clicked the nodeName is no longer an empty string so it wont render again with the new data.问题是一旦单击名称, nodeName 不再是空字符串,因此它不会再次使用新数据呈现。 Here is how I am getting the name + the API call to back end.这是我如何获得名称 + API 调用后端。

 data() {
      return {
          nodeName: '',
          tableName: null,
   }
}
    getNodeClicked() {
        window.addEventListener('sankey_node_clicked', (e) => { (this.nodeName = e.detail.name) })
      },
      async getTableData() {
        const nodeData = this.dateRange
        ? await get('nodeData', {
            dateStart: this.dateRange[0],
            dateEnd: this.dateRange[1],
            nodeName: this.nodeName
        }) : await get('nodeData', {
            nodeName: this.nodeName
          });
          console.log(nodeData)
        this.valuesCardData.push(nodeData);
      },  
    },

You can play with the :key attribute on your component to force the re-rendering.您可以使用组件上的:key属性来强制重新渲染。

There's a whole article written by Michael Thiessen about re-rendering: Michael Thiessen 写了一篇关于重新渲染的整篇文章:

https://michaelnthiessen.com/force-re-render/ https://michaelnthiessen.com/force-re-render/

The only thing you have to do is to put the key on your table-details (key should (or need to?) be put on components, not native HTML elements), and decide what you want to use as a key (could be an id, or your node name, or a simple number you increment when you press a button)你唯一需要做的就是把钥匙放在你的table-details上(钥匙应该(或需要?)放在组件上,而不是原生 HTML 元素上),然后决定你想用什么作为钥匙(可能是一个 id,或者你的节点名称,或者你按下按钮时增加的简单数字)

Your code should work perfectly, It's all about how nodeName value is getting update.您的代码应该可以完美运行,这完全取决于nodeName值如何更新。

Here is the working demo, Please have a look.这是工作演示,请看一下。 It will help you in finding the root cause.它将帮助您找到根本原因。

 Vue.component('child', { props: ['childmsg'], template: '<div>{{ childmsg }}</div>' }); var app = new Vue({ el: '#app', data: { nodeName: 'alpha' }, mounted() { setTimeout(() => { this.nodeName = 'beta'; }, 5000); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-if="nodeName:= ''"> <child :childmsg="nodeName"> </child> </div> </div>

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

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