vue子组件向父组件传递数据
Posted layfork
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue子组件向父组件传递数据相关的知识,希望对你有一定的参考价值。
子组件 <template> <div id="header"> <input type="text" v-model="username" @change="setUser"> </div> </template> <script type="text/ecmascript-6"> export default{ props: { }, data() { return { username:‘‘ } }, created() { }, methods: { setUser:function () { this.$emit(‘transferUser‘,this.username); } }, components: {}, computed: {} } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="stylus" rel="stylesheet/stylus"> </style>
父组件 <template> <div id="app"> <v-header @transferUser="getUser"></v-header> <div>用户名{{user}}</div> </div> </template> <script type="text/ecmascript-6"> import header from ‘components/header/header.vue‘; export default{ props: {}, data() { return { user:‘‘ } }, created() { }, methods: { getUser(msg){ this.user=msg; } }, components: { ‘v-header‘:header, }, computed: {} } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="stylus" rel="stylesheet/stylus"> </style>
以上是关于vue子组件向父组件传递数据的主要内容,如果未能解决你的问题,请参考以下文章
Vue父子组件通信(父级向子级传递数据子级向父级传递数据Vue父子组件存储到data数据的访问)