美文网首页
vue2父子组件通信

vue2父子组件通信

作者: BrotherWy | 来源:发表于2017-03-19 18:30 被阅读0次

前言

因为vue2去除了父子组件的双向数据绑定,所以在子组件是不允许修改父组件的属性的,控制台会报如下错误:

[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: "result"     (found in component )

解决办法

在子组件data中定义一个父组件传递过来的副本,再把该副本利用this.$emit("","")给传回去,父组件利用自定义事件接受该值

子组件

<template>
<div class="popup-bg" v-bind:class="{active:show}">
    <div class="popup">
        <div class="info">
            {{content}}
        </div>
    </div>
</div>
</template>

<script type="text/ecmascript-6">
    export default{
        data(){
            return {
                timers: [],
                show: this.isActive
            }
        },
    props: {
        content: {},
        isActive: {
            type: Boolean
        },
    },
    created(){

    },
    methods: {
        autoClose(){
            const t = setTimeout(() => {
                this.show = false;
            }, 1000);
            this.timers.push(t)
        }
    },
    watch: {
        isActive: function (val) {
            //清空一下timers
            if(this.timers){
                this.timers.forEach(timer=>{
                    window.clearTimeout(timer);
                })
            }
            this.show = val
            this.timers = [];
            this.autoClose();
        },
        show:function (val) {
            this.$emit("on-result-change",val);//发送副本给父组件
        }

    }
    }
</script>

父组件

   <template>
    <div class="index">
        <v-header :isActive="false" :title="title"></v-header>
        <div class="content">
            <button @click="show_">点击</button>
            <recruit-info></recruit-info>
        </div>
        <div class="footer">
            <v-footer></v-footer>
        </div>
        <popup :content="content" :isActive="show" @on-result-change="onResultChange">     </popup>
    </div>
  </template>
   <script type="text/ecmascript-6">
      import header from '../../component/header/header.vue';
      import footer from '../../component/footer/footer.vue';
      import recruitInfo from '../../component/recruit-info/recruit-info.vue'
      import popup from '../../component/popup/popup.vue'
      export default{
         data(){
             return {
                title: String,
                 show: Boolean,
                 content: "",

            }
        },
        created (){
            this.title = "拉勾网";
            this.show = false

        },
        components: {
            "v-header": header,
            "v-footer": footer,
            "recruit-info": recruitInfo,
            "popup": popup
        },
        methods: {
            show_: function () {
                this.content = "hello world";
                this.show = true
            },
            onResultChange(val){//自定义事件接受子组件传递过来的参数
                this.show = val
            }
        }

        }
    </script>
    <style lang="stylus" rel="stylesheet/stylus">
    .index
        position: relative
        height: 100%
        .content
            overflow: auto
            overflow-x: hidden
            padding-bottom: 45px
        .footer
            position: fixed
            bottom: 0
            height: 45px
            width: 100%

    </style>
    ```

相关文章

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • vue文档集合

    Vue2使用笔记3--父子组件的通信Vue 2.0开发实践(组件间通讯)Event Bus 在Vue中的使用vue...

  • 组件通信

    组件关系 组件关系可以分为父子组件通信、兄弟组件通信、跨级组件通信。 父子组件通信 1. 子组件使用 $emit(...

  • vue2父子组件通信

    前言 因为vue2去除了父子组件的双向数据绑定,所以在子组件是不允许修改父组件的属性的,控制台会报如下错误: 解决...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • [Vue]组件之间如何通信?

    组件通信可以大致分为两种情况,父子组件之间的通信和非父子组件之间的通信,下面来分别介绍 一、父子组件之间的通信 1...

  • Vue入门系列(五)组件通信

    组件内通信主要分为两种:父子组件通信和子组件之间通信。 1.父子组件通信 父组件通过props属性向子组件传递数据...

  • Vue3组件化(二):非父子组件的通信、插槽Slot

    非父子组件的通信 在开发中,我们构建了组件树之后,除了父子组件之间的通信之外,还会有非父子组件之间的通信。这里我们...

  • Vue相关知识点

    1、vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不...

  • 12.组件化开发2-非父子组件之间通信-祖先和后代之间的通信

    在开发中,我们构建了组件树之后,除了父子组件之间的通信之外,还会有非父子组件之间的通信。非父子组件的通信又可以分为...

网友评论

      本文标题:vue2父子组件通信

      本文链接:https://www.haomeiwen.com/subject/ijhvnttx.html