Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the elements of [__ob__: Observer] in VueJS?

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

My Child component looks like this,

B.vue

import C from './c.vue'

export default{

    props:['info'],

    components:{
        '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,

[__ob__: Observer] array

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!

like image 298
CoderPJ Avatar asked Nov 27 '17 04:11

CoderPJ


People also ask

What is Vue JS?

What is Vue.js? Vue.js is a progressive JavaScript framework created by Evan You and the Vue core team with contributions from more than 230 community members. Vue is used in more than 870,000 projects and has more than 175,000 stars on GitHub. Vue.js is an approachable core library that focuses on the view layer only.

How to access the child components of a Vue component?

You can access the child components of a Vue.js component with this.$children You can also explore by doing a simple console.log (this) inside your component and it will show you all the properties of your Vue component instance. Hint: this.$el is undefined until mounted. If you want to initialize a variable do it in mount ()

What are refs in Vue JS?

What are refs in Vue.js? Refs are Vue.js instance properties that are used to register or indicate a reference to HTML elements or child elements in the template of your application. If a ref attribute is added to an HTML element in your Vue template, you’ll then be able to reference that element or even a child element in your Vue instance.

How to manipulate a DOM element in Vue JS?

You can manipulate a DOM element by defining methods on the element’s reference. For example, you could focus to an input element with this: In this way, refs can be used just like the document.querySelector('.element') in JavaScript or the $('.element') in jQuery. The $refs can be accessed both inside the Vue.js instance and outside of it.


2 Answers

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

like image 190
Jose Fernández Avatar answered Sep 18 '22 18:09

Jose Fernández


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

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

If you simply want to test the behavior and your goal was to pass the string info into your component 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://vuejs.org/v2/guide/components-props.html

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

like image 23
DominikAngerer Avatar answered Sep 19 '22 18:09

DominikAngerer