vue中父组件改变子组件中的data,子组件改变父组件中的data
一、父组件改变子组件的data父组件parent.vue<template> <div> <span style="height:100px;">父组件</span> <button @click="chang
·
一、父组件改变子组件的data
父组件parent.vue
- <template>
- <div>
- <span style="height:100px;">父组件</span>
- <button @click="change">按钮</button>
- <div style="margin-top:5px;">
- <childForm style="padding:30px 20px;" ref="child"></childForm>
- </div>
- </div>
- </template>
- <script >
- import childForm from '.../child'
- export default {
- data () {},
- methods: {
- // 点击按钮改变子组件name值
- change() {
- this.$refs.child.name="改变后的值"
- }
- }
- }
- </script>
子组件child.vue
- <template>
- <div>
- <span style="height:100px;">子组件</span>
- <div>{{name}}</div>
- </div>
- </template>
- <script >
- export default {
- data () {
- return {
- name:'未改变的子组件值'
- }
- },
- methods: {
- }
- }
- </script>
二、子组件改变父亲组件的data
父组件parent.vue
- <template>
- <div>
- <span style="height:100px;">父组件</span>
- <span>{{name}}</span>
- <div style="margin-top:5px;">
- <childForm style="padding:30px 20px;" :parent='this' ></childForm>
- </div>
- </div>
- </template>
- <script >
- import childForm from '.../child'
- export default {
- data () {
- return {
- name:'父组件值'
- }
- },
- methods: {
- }
- }
- </script>
子组件child.vue
- <template>
- <div>
- <span style="height:100px;">子组件</span>
- <button @click="change">按钮</button>
- </div>
- </template>
- <script >
- export default {
- data () {
- return {
- }
- },
- props: ['parent'],
- methods: {
- // 点击按钮取得父组件name值
- change(){
- console.log(this.parent.name)
- }
- }
- }
- </script>
更多推荐
已为社区贡献2条内容
所有评论(0)