1

I'm working on a Vue app in which my initial view (projects.vue) renders an array of objects (using v-for loop), in summary form; each object's summary has button links to a 'detail' view & and 'edit' view; the links use an object property (id) to select/render the corresponding detail view, and this is working fine. Now, since these links will occur in several other views, I made a buttons.vue component and import it in projects.vue, but this way they're not getting the ID, and as a newb despite some reading about components and props I haven't figured how to achieve this seemingly simple function. My components:

projects.vue (this works; note the use of 'project.id'):

<template>
    <ol class="projects">
        <li class="project" v-for="(project, ix) in projects">
            <!— SNIP: some html showing the project summary  —>
            <!--
                my objective is to replace the following with
                <app-buttons></app-buttons>
            -->
            <div class="buttons">
                <router-link class="button" v-bind:to="'/project-detail/' + project.id">details</router-link>
                <router-link class="button" v-bind:to="'/project-edit/' + project.id">edit</router-link>
            </div><!-- END .buttons -->
        </li>
    </ol>
</template>


<script>

    export default {
        components: {
            'app-buttons': Buttons
        },
        data () {
            return {
                projects: [],
                search: "",
                projectID: ""
            }
        }, // data
        methods: {
        }, // methods
        created: function() {
            this.$http.get('https://xyz.firebaseio.com/projects.json')
            .then(function(data){
                return data.json();
            }).then(function(data){
                var projectsArray = [];
                for (let key in data) {
                    data[key].id = key; // add key to each object
                    this.projectID = key;
                    projectsArray.push(data[key]);
                }
                this.projects = projectsArray;
            })
        }, // created
        computed: {
        }, // computed
    } // export default
</script>

buttons.vue:

<template>
    <div class="buttons">

        <router-link class="button" v-bind:to="'/project-detail/' + projectID">details</router-link>
        <router-link class="button" v-bind:to="'/project-edit/' + projectID">edit</router-link>

    </div><!-- END .buttons -->
</template>

<script>

   export default {
        props: [ "projectID" ],
      data() {
         return {
         }
        },
        methods: {}
   }
</script>
1
  • 1
    Can you just bind them as props like :projectId="ix" and then call this.projectId from within the child component (after entering prop name)? Commented Sep 15, 2017 at 16:35

1 Answer 1

2

<app-buttons :projectId="foo"></app-buttons> then inside your component,

<template>
    <div class="buttons">

       <p>{{ this.projectId}}</p>

        <router-link class="button" v-bind:to="'/project-detail/' + projectID">details</router-link>
        <router-link class="button" v-bind:to="'/project-edit/' + projectID">edit</router-link>

    </div><!-- END .buttons -->
</template>

<script>

   export default {
        props: [ "projectId" ],
      data() {
         return {
         }
        },
        methods: {}
   }
</script>

Should show your project Id within the <p></p> tags.

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

1 Comment

Thank you! What I needed was the proper binding on the buttons: <app-buttons :projectID="project.id"></app-buttons> which I discover with your help - greatly appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.