vue组件通信

前言

引用自vue.js官网:

组件系统是 Vue 的另一个重要概念,因为它是一种抽象,允许我们使用小型、独立和通常可复用的组件构建大型应用。仔细想想,几乎任意类型的应用界面都可以抽象为一个组件树:

组件化开发是vue非常重要的一个特征,组件是相互独立的,但是许多的组件需要结合在一起,才能组成我们的大型应用,在构建过程中,组件之间必须通过通信,也就是传参来实现页面的功能。

一、父传子

1.父组件

<template>
  <div class="box">
    <div class="father">
      <p>我是父组件</p>
    </div>
    <Son :list="list"></Son>
  </div>
</template>

<script>
import Son from "@/components/son/index.vue";
export default {
  components: { Son },
  data() {
    return {
      list: [
        { name: "zhangsan", age: 18 },
        { name: "lisi", age: 19 },
        { name: "wangwu", age: 20 },
      ],
    };
  },
};
</script>

<style scoped lang='scss'>
.box {
  width: 1200px;
  height: 500px;
  display: flex;
  justify-content: space-around;
}
.father {
  width: 45%;
  height: 100%;
  border: 1px solid skyblue;
  border-radius: 20px;
  p {
    text-align: center;
  }
}
</style>

2.子组件

<template>
  <div class="son">
    <p>我是子组件</p>
    <hr />
    <p>这些是父组件传过来的数组</p>
    <ul>
      <li v-for="(item, index) in list" :key="index">
        {{ index + 1 }}:{{ item.name }}-----{{ item.age }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ["list"],
};
</script>

<style scoped lang='scss'>
.son {
  width: 45%;
  height: 100%;
  border: 1px solid pink;
  border-radius: 20px;
  p {
    text-align: center;
  }
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    width: 100%;
    height: 30px;
    line-height: 30px;
    margin: 10px 180px;
  }
  .el-button {
    margin: 30px 220px;
  }
}
</style>

二、子传父

1.父组件

<template>
  <div class="box">
    <div class="father">
      <p>我是父组件</p>
      <hr />
      <p>data变量Count是子组件传过来的值:{{ count }}</p>
    </div>
    <Son :count="count" @addCounts="addCounts"></Son>
  </div>
</template>

<script>
import Son from "@/components/son/index.vue";
export default {
  components: { Son },
  data() {
    return {
      count: 0
    };
  },
  methods: {
    addCounts(count) {
      this.count = count += 1;
    },
  },
};
</script>

<style scoped lang='scss'>
.box {
  width: 1200px;
  height: 500px;
  display: flex;
  justify-content: space-around;
}
.father {
  width: 45%;
  height: 100%;
  border: 1px solid skyblue;
  border-radius: 20px;
  p {
    text-align: center;
  }
}
</style>

2.子组件

<template>
  <div class="son">
    <p>我是子组件</p>
    <hr />
    <p>这个按钮是向父组件传值</p>
    <el-button type="primary" @click="addCount">Count++</el-button>
  </div>
</template>

<script>
export default {
  props: ["count"],
  methods: {
    addCount() {
      this.$emit("addCounts", this.count);
    },
  },
};
</script>

<style scoped lang='scss'>
.son {
  width: 45%;
  height: 100%;
  border: 1px solid pink;
  border-radius: 20px;
  p {
    text-align: center;
  }
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    width: 100%;
    height: 30px;
    line-height: 30px;
    margin: 10px 180px;
  }
  .el-button {
    margin: 30px 220px;
  }
}
</style>

三、完整代码

1.父组件

<template>
  <div class="box">
    <div class="father">
      <p>我是父组件</p>
      <hr />
      <p>data变量Count是子组件传过来的值:{{ count }}</p>
    </div>
    <Son :list="list" :count="count" @addCounts="addCounts"></Son>
  </div>
</template>

<script>
import Son from "@/components/son/index.vue";
export default {
  components: { Son },
  data() {
    return {
      count: 0,
      list: [
        { name: "zhangsan", age: 18 },
        { name: "lisi", age: 19 },
        { name: "wangwu", age: 20 },
      ],
    };
  },
  methods: {
    addCounts(count) {
      this.count = count += 1;
    },
  },
};
</script>

<style scoped lang='scss'>
.box {
  width: 1200px;
  height: 500px;
  display: flex;
  justify-content: space-around;
}
.father {
  width: 45%;
  height: 100%;
  border: 1px solid skyblue;
  border-radius: 20px;
  p {
    text-align: center;
  }
}
</style>

2子组件

<template>
  <div class="son">
    <p>我是子组件</p>
    <hr />
    <p>这些是父组件传过来的数组</p>
    <ul>
      <li v-for="(item, index) in list" :key="index">
        {{ index + 1 }}:{{ item.name }}-----{{ item.age }}
      </li>
    </ul>
    <hr />
    <p>这个按钮是向父组件传值</p>
    <el-button type="primary" @click="addCount">Count++</el-button>
  </div>
</template>

<script>
export default {
  props: ["list", "count"],
  methods: {
    addCount() {
      this.$emit("addCounts", this.count);
    },
  },
};
</script>

<style scoped lang='scss'>
.son {
  width: 45%;
  height: 100%;
  border: 1px solid pink;
  border-radius: 20px;
  p {
    text-align: center;
  }
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    width: 100%;
    height: 30px;
    line-height: 30px;
    margin: 10px 180px;
  }
  .el-button {
    margin: 30px 220px;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值