2

I am trying to use a component to render a Table Row using VueJS, but I cannot read the properties of the object when doing so. I have two components: ProspectsIndex and Player.

Here is the ProspectsIndex template:

<template>
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Player</th>
                </tr>
            </thead>
            <tbody>
                <tr 
                    v-for="(player, index) in prospects"
                    v-bind:player="player"
                    v-bind:index="index"
                    v-bind:key="player.personId"
                    is="player"
                    >
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script>
    import Player from './Player';
    export default {
        name: 'ProspectsIndex',
        components: {
            Player
        },
        data() {
            return {
                position: '-',
                status: '-',
                name: '',
                schools: this.$parent.$parent.schools,
                prospects: this.$parent.$parent.prospects
            };
        }
    }
</script>

This was working just fine until I tried splitting the row into its own component. I am doing this to have some help with computed properties and other things, scaling, separation of concerns etc. Here is the Player component:

<template>
    <tr>
        <td valign="top" width="5%" style="padding:0">
            {{player.lastName}}
        </td>
    </tr>
</template>

<script>
    export default {
        name: 'Player'
    }
</script>

I have tried a number of different ways to get the player object into the Player component to no avail. Here is what the player object looks like:

{
   "personId":2559901,
   "firstName":"SAQUON",
   "lastName":"BARKLEY",
   "pickAnalysis":null,
   "hasAnalysis":false,
   "video":null,
   "pick":null,
   "college":38,
   "pos":"RB",
   "height":"6'0\"",
   "weight":233,
   "armLength":"31 3/8",
   "handSize":"9 1/2",
   "expertGrade":7.45,
   "fanPick":null,
   "schoolYear":"Junior",
}

Now, if in the Player component I replace {{player.lastName}} with "testing" I see many hundreds of rows of testing printed. So that portion works, its just accessing the player object which has me stumped!

5
  • Wouldn't you pass the player to your component via a prop? Commented Aug 29, 2018 at 3:00
  • Would I Phil? I am not exactly sure, which is why I am asking ;-) I don't think I've fully understood the point of props just yet as I am a Vue noob. Commented Aug 29, 2018 at 3:04
  • 1
    props is how you get data from an outer scope into a component. See vuejs.org/v2/guide/… Commented Aug 29, 2018 at 3:07
  • Awesome, should I be using props instead of doing stuff like this then: data() { return { prospects: this.$parent.$parent.prospects }; }, Commented Aug 29, 2018 at 14:21
  • 1
    Absolutely! And for communicating back out, use $emit(). See vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow Commented Aug 29, 2018 at 22:53

1 Answer 1

1

In your Player component, add a prop to accept the player object from the parent scope, ie

export default {
  name: 'Player',
  props: {
    player: Object
  }
}

You already have v-bind:player="player" in your parent scope so that should be all you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.