admin管理员组文章数量:1394518
iam practising vuejs transfering data between ponents and this is my code
My blade template:
`<app-user-details :name="name"></app-user-details>`
Then in my Child-Component:
<template>
<div>
<h4>You may view the user details here</h4>
<p>Many Details</p>
<p>User name: {{ name }}</p>
<button @click="resetName">reset name</button>
</div>
</template>
<script>
export default {
name: 'app-user-datails',
props: {
name: {
type: String
}
},
methods: {
resetName() {
return this.name = 'Max'
}
}
}
</script>
i want to change the name in props by the method resetName() but in the console i get this error
vue.js:1355 Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'name'
what should i do?!
iam practising vuejs transfering data between ponents and this is my code
My blade template:
`<app-user-details :name="name"></app-user-details>`
Then in my Child-Component:
<template>
<div>
<h4>You may view the user details here</h4>
<p>Many Details</p>
<p>User name: {{ name }}</p>
<button @click="resetName">reset name</button>
</div>
</template>
<script>
export default {
name: 'app-user-datails',
props: {
name: {
type: String
}
},
methods: {
resetName() {
return this.name = 'Max'
}
}
}
</script>
i want to change the name in props by the method resetName() but in the console i get this error
vue.js:1355 Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'name'
what should i do?!
Share Improve this question asked Nov 20, 2020 at 14:07 Mohamed MamdohMohamed Mamdoh 611 silver badge2 bronze badges1 Answer
Reset to default 6You should not try to update a prop for inside a child ponent, from the docs :
All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child ponents from accidentally mutating the parent’s state, which can make your app’s data flow harder to understand.
However, you can emit an event with the new name from the child ponent :
resetName() {
this.$emit('updateName', 'Max')
}
And update it from the parent by listening to the event:
<app-user-details :name="name" @updateName="name = $event"></app-user-details>
本文标签: javascriptHow can I change the value of props in vue with a method in a componentStack Overflow
版权声明:本文标题:javascript - How can I change the value of props in vue with a method in a component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744100696a2590855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论