I solved the problem, moving everything into one component. But if someone answers my question, I will be grateful.
Oct 27, 2017
6
Level 1
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders
Hello guys. Tell me, please, what I'am doing wrong.
Items.vue
<template>
<el-table
:data="items"
border
style="width: 100%">
<el-table-column
prop="name"
label="Name">
</el-table-column>
<el-table-column
prop="brand"
label="Brand">
</el-table-column>
<el-table-column
prop="status"
label="Status"
sortable>
<template slot-scope="scope">
<status-select
:itemid="scope.row.id"
:itemstatus="scope.row.status">
</status-select>
</template>
</el-table-column>
</el-table>
</template>
<script>
import Table from 'element-ui/lib/table';
import TableColumn from 'element-ui/lib/table-column';
export default {
components: {
'el-table': Table,
'el-table-column': TableColumn
},
data() {
return {
items: [{
name: '',
brand: '',
status: ''
}]
}
},
methods: {
getItems() {
axios.get('/api/items')
.then(response => {
this.items = response.data.items;
})
.catch(error => {
console.log(error.response.data);
});
}
},
mounted() {
this.getItems();
}
}
</script>
StatusSelect.vue
<template>
<el-select v-model="status" @change="setStatus" placeholder="Check status">
<el-option label="Sale" value="sale"></el-option>
<el-option label="Ordered" value="ordered"></el-option>
<el-option label="Paid" value="paid"></el-option>
</el-select>
</template>
<script>
import Select from 'element-ui/lib/select';
import Option from 'element-ui/lib/option';
export default {
props: ['itemid', 'itemstatus'],
components: {
'el-select': Select,
'el-option': Option
},
data() {
return {
}
},
computed: {
status: {
get() { return this.itemstatus },
set(value) {
this.itemstatus = value;
}
}
},
methods: {
setStatus() {
axios.patch('/api/items/'+this.itemid+'/status', { 'status': this.itemstatus })
.then(response => {
swal({
buttons: false,
text: response.data.message,
icon: 'success',
timer: 1000
});
})
.catch(error => {
console.log(error.response.data);
});
}
},
created() {
this.status = this.itemstatus;
}
}
</script>
I get this error: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "itemstatus".
Level 1
Problem solved.
I replaced it...
items: [{
name: '',
brand: '',
status: ''
}],
...with this one.
items: [],
Thank you guys for the answers.
1 like
Please or to participate in this conversation.