vue子父之间传值方式

子传父

  vue子传父使用$emit传值

  子组件:

<template>
    <div>
        <button @click="toParent">点击传到父级</button>
    </div>
</template>
<script>
export default {
    name: 'child',
    methods: {
        toParent () {
            this.$emit('fromChild', 'child')
        }
    }
}
</script>

  

 

 父组件

<template>
    <div>
        <p>子级传过来的值:{{childVal}}</p>
        <child @fromChild="getChild"></child>
    </div>
</template>
<script>
import child from "@/components/child";
 
export default {
    name: 'parent',
    data () {
        return {
            childVal: ''
        }
    },
    components: {
        child
    },
    methods: {
        getChild (v) {
            this.childVal = v;
        }
    }
}
</script>

  

 

 

传过来啦

父传子 

  子组件使用props接收 接收时还可以设置默认值 当没获取到值时 会使用设置的默认值

 父组件

 

<template>
    <div>
        <child :tochild="parentVal"></child>
    </div>
</template>
<script>
import child from "@/components/child";
 
export default {
    name: 'parent',
    data () {
        return {
            parentVal: 'parent',
        }
    },
    components: {
        child
    }
}
</script>

  

 

 

 子组件

<template>
    <div>
        <p>父级传过来的值:{{tochild}}</p>
    </div>
</template>
<script>
export default {
    name: 'child',
    props: {
        tochild: String
    }
}
</script>

  

 

 

vue 自定义组件(父级,子级组件)传参

父组件:

<template>
    <div>
        <p>我在父组件</p>
        <p @click="alertClick('值')">点击向子组件传值</p>
        <alert v-on:Event_s="backRequest" ref="alert"></alert>
        //ref 声明组件  v-on回调事件名
    </div>
</<template>
<script>
import material from 'alert';//引入子组件,子组件路径
export default {
    data() {
        return {}
    },
    //注册组件
    components: {
        alert: alert,
    },
    methods: {
        //使用this.$refs 调用alert组件 的myAlert方法
        alertClick(val){
          this.$refs.alert.myAlert(val);
        },
        backRequest: function (val) { //接收子组件返回的值,val值应该为:我是子组件返回给父组件的内容
          alert(val)
        },
    },
}
</script>

  

 

子组件:

<template>
    <p>我在子组件</p>
</template>
<script>
export default {
    data() {
        return {}
    },
    methods: {
        myAlert(val){
            alert(val);
            this.return('我是子组件返回给父组件的内容')//调用下面return方法
        },
        //使用 this.$emit 返回给父级组件内容
        return(data) {
          this.$emit('Event_s',data);//Event_s 为父级组件用v-on声明的名称
        },
    },
}
</script>

  

Vue高级组件传值之 provide/inject

父组件
<script>
  export default {
    name: "parent",
    provide() {
      return {
        parent:'Hellow World',
        hello:this.hello
      }
    },
    methods:{
      hello(){
        console.log("Hello World")
      },
    }
  };
</script>
子组件
<template>
  <div>{{parent}}</div>
</template>

<script>
  export default {
    name: "great-grandson",
    inject:["parent","hello"],
    mounted() {
      this.hello();
    }
  };
</script>

 子组件调用父组件方法

父组件

<template>
  <div>
    <child :fatherMethod="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '@/components/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('fatherMethod');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod">点击</button>
  </div>
</template>
<script>
  export default {
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
        if (this.fatherMethod) {
          this.fatherMethod();
        }
      }
    }
  };
</script>

 

posted @ 2020-11-09 14:19  磊~~  阅读(456)  评论(0编辑  收藏  举报