在vue.js组件中对道具进行操作

kof*_*ola 1 html javascript vue.js vue-component

我对vue相当陌生,我试图将laravel项目的前端迁移到vue,但是我遇到了问题,我试图遍历一组提供的对象(称为房间),并为每个对象创建div。以及将默认的room_id设置为房间的第一个ID。问题是,当在dom(html)中访问提供的名为“ room”的prop数组时,它可以正常工作,但是在我的组件文件的vue代码中,它似乎始终是未定义或为空这是我的组件vue代码:

export default {
    created() {
        //this.loadMessages(this.room_id)
        console.log(this.first_room)  //undefined;
        console.log(this.rooms) //empty array;
    },
    props: ['rooms','first_room'],
    computes:{
        myrooms: function(){
            return this.first_room;
        }
    },
    data()
    {
        return{
            messages: [],
            newMessage: '',
            room_id: 1 //for test purposes, this works,
        }
    },
    methods: {
        loadMessages(id)
        {
            axios.get('/messages/'+id).then(response => {
                this.messages = response.data;
                console.log(response.data);
            });

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

组件html的重要部分

<div v-for="room in rooms">
    <div class="chat-user room">
        <div v-for="other in room.others">
            <img class="chat-avatar img-circle" :src="other.image" alt="image" >                                        
            <div class="chat-user-name">
                <a :href="'/user/' + other.id">{{ other.name}}</a>
            </div>
        </div>
    </div>
</div>
//this all works, just for reference
Run Code Online (Sandbox Code Playgroud)

我在主Vue实例中设置传递给prop的值的方法

编辑:父实例代码 哦,我似乎也无法访问正在传递的rooms数组,因为它始终是空的IN代码,但它在html中循环

window.App.app= new Vue({
el: '#wrapper',
data: {
    messages: [],
    rooms: [],
    test: 'yes',
    first: ''
},
created() {
    this.fetchRooms();
    this.first = 1;
},
methods: {
    fetchMessages(id) {
        console.log(id);
    },
    fetchRooms()
    {
        axios.get('/rooms').then(response => {
            this.rooms = response.data;
        });
    }
}
});
Run Code Online (Sandbox Code Playgroud)

终于我在哪里叫我的组件

<chat-messages :rooms="rooms" :first_room="1"></chat-messages>
//variables referenced from main vue instance
Run Code Online (Sandbox Code Playgroud)

我确实已经把我的大部分头发扯了,请给我任何帮助

Amr*_*pal 5

在子元素中传递道具。

export default {
  created() {
    console.log(this.first_room)  //undefined;
  },
  props: ['rooms','first_room'],
  computed :{
    myrooms: function(){
        return this.first_room;
    }
  },
  data () {
    return {
        messages: [],
        newMessage: '',
        room_id: 1 //for test purposes, this works,
    }
  },
  watch: {
    rooms (n, o) {
      console.log(n, o) // n is the new value, o is the old value.
    }
  },
  methods: {
    loadMessages (id) {
        axios.get('/messages/'+id).then(response => {
            this.messages = response.data;
            console.log(response.data);
        });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以对数据属性添加监视,也可以对其进行计算以查看其值的变化。

在问题中(看起来好像是这样),您已经consolecreated生命周期中获得了道具的值,在创建子组件之后,通过父组件中的API调用更改了道具的值。这就解释了为什么您的模板显示数据,而不显示console在创建的生命周期挂钩中。