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>
:projectId="ix"and then callthis.projectIdfrom within the child component (after entering prop name)?