最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access the elements of [__ob__: Observer] in VueJS? - Stack Overflow

programmeradmin3浏览0评论

I am fairly new to VueJS. There is a parent ponent, from which, data is passed to child and grandchild.

My Child ponent looks like this,

B.vue

import C from './c.vue'

export default{

    props:['info'],

    ponents:{
        'c': C
    },
    
    created: function(){
      this.getInfo();
    },

    methods: {
        getInfo: function(){
            console.log("Printing inside get method", this.info);
        }
    }
}
<template>
  <div>
    <c :info="info"></c>
  </div>
</template>

I am fairly new to VueJS. There is a parent ponent, from which, data is passed to child and grandchild.

My Child ponent looks like this,

B.vue

import C from './c.vue'

export default{

    props:['info'],

    ponents:{
        'c': C
    },
    
    created: function(){
      this.getInfo();
    },

    methods: {
        getInfo: function(){
            console.log("Printing inside get method", this.info);
        }
    }
}
<template>
  <div>
    <c :info="info"></c>
  </div>
</template>

When I see the console, I see an array printed like this,

When i try to access the elements of the array like this, info[0], the console shows undefined. I am unable to access the elements of the array. Can someone please help me out here? Thanks!

Share Improve this question edited Nov 27, 2017 at 4:13 CoderPJ asked Nov 27, 2017 at 4:04 CoderPJCoderPJ 1,0116 gold badges20 silver badges44 bronze badges 4
  • 1 The chrome inspector continues updating objects as the script runs. You could try logging a copy of the observable to see what I mean. You may need to either use a later lifecycle hook or subscribe to the observable to get the value you are looking for – Jami Couch Commented Nov 27, 2017 at 4:14
  • @JamiCouch I tried logging it in the mounted life cycle hook. I am still unable to access. it says undefined. – CoderPJ Commented Nov 27, 2017 at 4:22
  • what's the info – masongzhi Commented Sep 11, 2018 at 9:50
  • How and where exactly are you trying to access info[0] in your code? – Grillparzer Commented Aug 6, 2019 at 16:55
Add a ment  | 

6 Answers 6

Reset to default 1
<template>
  <div>
    <c :info="info"></c>
  </div>
</template>

The :info="info" will pass your outer ponents info property into the c ponent. If that outer ponent does not have a property info it will result in the undefined you can see right now (according to ments).

If you simply want to test the behavior and your goal was to pass the string info into your ponent c than you can pass it as a string by doing it like:

<template>
  <div>
    <c :info="'info'"></c>
  </div>
</template>

or without the ::

<template>
  <div>
    <c info="info"></c>
  </div>
</template>

Why? Because : is shorthand for v-bind: which looks for javascript objects and since :info="info" is equal to :info=info you actually want to go with :info="'info'" since this would be equal to: info='info'.

You can read more about how this works in the Props Doc section of Vue.js: https://v2.vuejs/v2/guide/ponents-props.html

If the info property is set in your outer ponent - let us know how so we can help you further.

In this case this.info is an :Observer because you are consoling the prop before it is fulfilled, in this exact case if you call this.getInfo() in the mounted() lifehook instead of created() you will be able to get the prop itself (as in the mounted() the props are already passed), and not the Observer.

So that's why you are able to see the object in the console as :Observer type and the content in it, but not this.info[0] as it is waiting for the prop to be passed.

Here you can find a threat talking more extensive about it : Vue JS returns [__ob__: Observer] data instead of my array of objects

One way to debug this issue is to use a chrome extension called Vue.js DevTools which can be found at https://chrome.google./webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en. That extension would allow you to inspect props passed into each of your ponents, and with that information you should be able to determine where in the chain of parent, child, grand-child your props are getting lost.

I found the solution in here:

https://forum.vuejs/t/how-to-access-the-elements-of---ob---observer-in-vuejs/22404/5

The child ponent doesn’t “wait” for axios to finish, so it’s initially rendered with an empty array for info.

If you want to wait for the data to be present before you render the child ponent, use v-if="info.length > 0" on the child ponent in the parent’s template.

Its working for me now, i wanted to share the solution i found.

If you are trying to access the contents of info on create, the prop is probably not passed yet. You would be better off checking it out on mounted.

You are seeing the content of info in the console since the output in the browser console gets updated. If you want to now what the contents of info is on create, print the JSON representation like this: JSON.stringify(this.info)

If the info es from a server or any async method the mounted still throw an error if the data isn't ready before your child ponent mounted.

If the child ponent required the info props I think the best way to conditional rendering:

<c v-if="info" :info="info"></c>

or somithing like that. If you want a loading state you should handle the undefined data inside the child ponent, rendering a loading state if the info is undefined and render the other contents when it will be available.

发布评论

评论列表(0)

  1. 暂无评论