简体   繁体   English

Vue.js 不呈现列表中的元素

[英]Vue.js not rendering elements in a list

so, for context, I have a client-side for an app where the server sends the client the client's friends over a socket connection.因此,对于上下文,我有一个应用程序的客户端,其中服务器通过套接字连接向客户端发送客户端的朋友。 I can retrieve the friends, and even create clickable list elements so that the user can send a message to a friend, etc. However the one problem that I am having is that when using the mustache syntax to render information, nothing renders.我可以检索朋友,甚至创建可点击的列表元素,以便用户可以向朋友发送消息等。但是,我遇到的一个问题是,当使用 mustache 语法来呈现信息时,什么都不呈现。 I have the following code.我有以下代码。

index.html (the following is inside the app div) index.html(以下在app div里面)

<div id="friends">
                <p>Friends</p>
                <ul id="friend-list">
                    <li 
                        v-for="friend in friends"
                        v-on:click="setFriend(friend.id)"
                        >Friend:{{friend.text}}
                    </li>
                </ul>
            </div>

index.js索引.js

var socket = io.connect('http://localhost:3000');
var app = new Vue({
    el: '#app',
    data: {
        messageContent: '',
        friends: [],
        messages: []
    }
})
socket.on('connect', function(){
  socket.emit('authentication', Cookies.get('user'));
  socket.on('authenticated', function() {
  socket.on('sendFriends',(data) => {
        data.forEach(element => {
            console.log(element.username)
            app.friends.push({
                text: element.username + '.'+element.id, 
                id: element.id
            })

        })
        toUser = app.friends[0].id
    })
})
})

Even if I were to fill the friends array without data from the socket connection, the text on each element would not render.即使我在没有来自套接字连接的数据的情况下填充朋友数组,每个元素上的文本也不会呈现。 By this I mean, there would be bullet points which have the on click function, however no text.我的意思是,会有具有点击功能的项目符号,但没有文本。 However, when I create a codepen, the data is rendered correctly.但是,当我创建 codepen 时,数据会正确呈现。 Does this have to do with the socket connection and how Vue reacts to data change?这是否与套接字连接以及 Vue 对数据更改的反应有关? https://codepen.io/dvub/pen/XWaebyb (codepen link) https://codepen.io/dvub/pen/XWaebyb(codepen链接)

Since API calls are asynchronous, Vue is already reading the values of friends before the call is processed and the data is received.由于 API 调用是异步的,在处理调用和接收数据之前,Vue 已经在读取friends的值。 So Vue is already started looping over the friends array before anything has come back from the API.因此,在 API 返回任何内容之前,Vue 已经开始循环访问friends数组。

The individual friend is passed as a prop to a child component.单个friend作为道具传递给子组件。 The child component inturn is now looking for properties on that object (ie, friend.name ).子组件现在正在寻找该对象的属性(即, friend.name )。

But the friends array is empty when the component is first mounted.但是当组件第一次挂载时,friends 数组是空的。 Therefore adding a simple v-if to stop the rendering until you have received the information is needed.因此需要添加一个简单的v-if来停止渲染,直到您收到信息。

<ul id="friend-list" v-if="friends.length">

You may also want to remove您可能还想删除

toUser = app.friends[0].id

It does not look like valid JS syntax (where is toUser declared?).它看起来不像有效的 JS 语法( toUser在哪里声明?)。

You can't set properly friends values unless you're into the Vue's app context.除非您进入 Vue 的应用程序上下文,否则您无法正确设置friends值。 So you should begin to put back all the stuff into your Vue app.所以你应该开始把所有的东西放回你的 Vue 应用程序中。 It should looks more like something (I can't guarantee it works since I don't know how your socket lib works, but this is how it should be structured) :它应该看起来更像一些东西(我不能保证它可以工作,因为我不知道你的套接字库是如何工作的,但它应该是这样构造的):

<div id="friends">
    <p>Friends</p>
    <ul id="friend-list">
        <li v-for="friend in friends"
            v-on:click="setFriend(friend.id)">Friend:{{friend.text}}
        </li>
    </ul>
</div>
const app = new Vue({
    el: '#app',
    data: {
        socket: null,
        messageContent: '',
        friends: [],
        messages: []
    },
    mounted: {
        // Here it depends on how the library works. You may have to use async / await on connection init :
        this socket = io.connect('http://localhost:3000');
        this.socket.on('connect', function(){
            this.socket.emit('authentication', Cookies.get('user'));
            this.socket.on('authenticated', function() {
            this.socket.on('sendFriends',(data) => {
                data.forEach(element => {
                    console.log(element.username)
                    this.friends.push({
                        text: element.username + '.'+element.id, 
                        id: element.id
                    });
                });
            });
            // I don't know what you're trying to do here so I commented this one :
            // toUser = app.friends[0].id
        });
    }
});

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

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