I am trying to pass an array of tags from the child component to the parent component using emit. The emit is registering and firing the method in the parent component, but when I log the argument that I passed via emit it returns undefined. I am not sure what I am doing wrong because I have passed objects via emit before.
I have tried trying to convert the array to an object, passing it as the this value of the same name without storing it as a variable within the method itself.
//edittagsform
<template>
<div class="card">
<div class="card-body">
<div class="form-inputs">
<h5>Add tags:</h5>
<tagsField v-on:incoming="updateTagsList()"/><br>
<h5>Current tags:</h5>
<div v-if="tags.length > 0">
<tagsList v-for="tag in tags" v-bind:tag="tag"/>
</div>
<div v-else>
<p>No tags associated</p>
</div>
</div>
</div>
</div>
</template>
<script>
import tagsField from './tagsField'
import tagsList from './tagsList'
export default {
data: function () {
return {
tags: {
tag: this.tagList,
}
},
props: ['tagList'],
name: 'editTagsForm',
methods: {
updateTagsList(newTag) {
console.log(newTag)
this.tags.push(newTag)
}
},
components: {
tagsField,
tagsList
}
}
</script>
<style scoped>
</style>
//tagsField.vue
<template>
<div class="input-group">
<form>
<input-tag v-model="newTags"></input-tag><br>
<input type="button" value="submit" v-on:click="addTag()" class="btn btn-secondary btn-sm">
</form>
</div>
</template>
<script>
export default {
data: function () {
return {
newTags: [],
}
},
methods: {
addTag() {
for(var i = 0; i <= this.newTags.length; i++){
var arr = this.newTags[i]
this.$emit('incoming', {
arr
})
}
}
}
}
</script>
<style scoped>
</style>```
//tagsField
<template>
<div class="input-group">
<form>
<input-tag v-model="newTags"></input-tag><br>
<input type="button" value="submit" v-on:click="addTag()" class="btn btn-secondary btn-sm">
</form>
</div>
</template>
<script>
export default {
data: function () {
return {
newTags: [],
}
},
methods: {
addTag() {
const newTags = this.newTags;
this.$emit('incoming', newTags)
this.newTags = []
}
}
}
</script>
<style scoped>
</style>
//edittagsform
<template>
<div class="card">
<div class="card-body">
<div class="form-inputs">
<h5>Add tags:</h5>
<tagsField v-on:incoming="updateTagsList"/><br>
<h5>Current tags:</h5>
<div v-if="tags.length > 0">
<tagsList v-for="tag in tags" v-bind:tag="tag"/>
</div>
<div v-else>
<p>No tags associated</p>
</div>
</div>
</div>
</div>
</template>
<script>
import tagsField from './tagsField'
import tagsList from './tagsList'
export default {
data: function () {
return {
tags: this.tagList
}
},
props: ['tagList'],
name: 'editTagsForm',
methods: {
updateTagsList(newTags) {
console.log(newTags)
for(var i = 0; i < newTags.length; i++) {
console.log(i)
this.tags.push(newTags[i])
}
}
},
components: {
tagsField,
tagsList
}
}
</script>
<style scoped>
</style>