【问题标题】:v-for loop print empty row after runv-for 循环在运行后打印空行
【发布时间】:2021-06-12 13:44:03
【问题描述】:

我正在处理 vue 组件文件,我通过 laravel WebSockets 获取数据,现在我想在模板标签中的表格中打印所选数据,但是我的行在表格中打印为空白,行在增加但为空数据。请让我知道我在哪里弄错了。


    <template>
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">Example Component</div>
    
                        <div class="card-body">
                            <table border="1">
                                <tr>
                                    <th>Email</th>
                                    <th>Address</th>
                                </tr>
                                <tr v-for="list in Items" :key="list.id">
                                    <td> {{list.email}} </td>
                                    <td> {{list.address}} </td>
                                </tr>
                            </table> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </template>
    
    <script>
    import axios from "axios";
    import Echo from "laravel-echo";
    
        export default {
            data(){
                return{
                    Items:[]
                }
            },
            mounted() {
                this.FetchItems();
            },
            methods:{
                FetchItems(){
                   window.Echo.channel('lists')
                   .listen('DataUpdate',(e) =>{
                        this.Items = e.lists;
                        console.log(this.Items); 
                   });
               }
            }
        }
    </script>

【问题讨论】:

  • 这行console.log(this.Items)给你什么?
  • 我更喜欢在created hook 中调用 API。
  • @Saeed 它不会有太大的不同。
  • 另外,如果您在代码中使用window.Echo,这个导入import Echo from "laravel-echo"; 会做什么?
  • @AdarshMohan console.log(this.Items) 正在获取 json 格式的数据。

标签: javascript laravel vue.js v-for laravel-websockets


【解决方案1】:

我的问题解决了, 我需要将数据推送到变量中,而不需要分配给它。

Listen(){
    window.Echo.channel('lists')
    .listen('DataUpdate',(e) =>{
    this.Items.push(e.lists);
    });
   }

【讨论】:

  • 这取决于需求,如果你想要比当前数据更多的数据,你需要推送它,如果你想替换它们,你需要分配它。不管怎样,很高兴看到你解决了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多