Skip to main content
  1. About
  2. For Teams
Asked
Viewed 107 times
1

I am taking values from a Modal using below code.

<Modal :show="showModal" @hide="showModal = false" @submitted_job_data ="data_submitted"></Modal>

My Vue.js code is like below

<script>
  import Modal from './Modal';
  export default {
    data() {
      return {
        showModal: false,
        jobs: [],
      }
    },
    components: { Modal },
    methods: {
      data_submitted(value) {
        this.jobs.push(value);
        console.log(Object.values(this.jobs)); 
      }
    }
  }
</script>

I am iterating vales like below

<tbody>
   <tr v-for="job in jobs" :key="job.id">
    <td>
     {{ job[0] }}
    </td>
    <td>
     {{ job[1] }}
    </td>
    <td>
     {{ job[2] }}
    </td>
    <td>
     {{ job[3] }}
    </td>
    <td>
     {{ job[4] }}
     </td>
  </tr>
</tbody>

I am getting console result like below

enter image description here

Why I am getting Getter & Setter instead of value ?

3
  • 1
    What is the problem exactly? How does what you want to achieve differ from what you currently have?
    skirtle
    –  skirtle
    2019-08-11 03:45:58 +00:00
    Commented Aug 11, 2019 at 3:45
  • Thanks @skirtle. Why I am getting Getter & Setter instead of value ?
    abu abu
    –  abu abu
    2019-08-11 03:56:47 +00:00
    Commented Aug 11, 2019 at 3:56
  • 1
    Object.values() should be apply on objects not on arrays, i mean jobs is a array.
    Christian Carrillo
    –  Christian Carrillo
    2019-08-11 04:00:16 +00:00
    Commented Aug 11, 2019 at 4:00

2 Answers 2

2

Vue rewrites object properties to use a getter and setter so it can track when they are accessed.

https://v2.vuejs.org/v2/guide/reactivity.html

If you expand the object in the console you'll still be able to access the underlying value. The value isn't shown automatically because calling the getter could have side-effects.

As far as your other code is concerned you don't need to worry about the getter and setter, just use the objects like you normally would. e.g.:

<td>{{ job.company_name }}</td>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @skirtle for your reply. Could you please say how can I iterate over the objects ?
@abuabu Not sure I understand--you already are iterating over the objects. Which objects are you talking about?
Thanks @DaveNewton. I am not getting any output in browser while iterating.
@abuabu I have updated my answer with a concrete example of outputting a value.
Thanks @skirtle. I tried your solution before posting this question but it is not working. Thanks.
1

The v-for loop you do in your Template works and iterates the object, but the elements below are not correct. It should look like this:

<tr v-for="job in jobs">
  <td :key="job.id">
   {{ job.company_name }}
  </td>
</tr>

The getters and setters don't matter to you, you can ignore them and access the values in the object like usual: {{ job.company_name }}. It's an internal thing Vue does.

Working example: https://codepen.io/reynicke/pen/PMyLBb

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.