Vue 组件通信

组件通信

组件与组件之间的数据传递

组件的数据是独立的,无法直接访问其他组件的数据。通过组件通信,可以访问其他组件的数据。

组件关系

  1. 父子关系
  2. 非父子关系

组件通信解决方案

父子关系

父->子

父组件通过props将数据传递给子组件

App.vue

<template>
  <div class="app" style="border: 3px solid #000; margin: 10px">
    我是APP组件
    <!-- 1.给组件标签,添加属性方式 赋值 -->
    <Son :title="myTitle"></Son>
  </div>
</template>

<script>
import Son from './components/Son.vue'
export default {
  name: 'App',
  data() {
    return {
      myTitle: 'Hello Vue',
    }
  },
  components: {
    Son,
  },
}
</script>

<style>
</style>

Son.vue

<template>
  <div class="son" style="border:3px solid #000;margin:10px">
    <!-- 3.直接使用props的值 -->
    我是Son组件 {{title}}
  </div>
</template>

<script>
export default {
  name: 'Son-Child',
  // 2.通过props来接受
  props:['title']
}
</script>

<style>

</style>
子->父

子组件利用$emit通知父组件修改更新

Son.vue
<template>
  <div class="son" style="border: 3px solid #000; margin: 10px">
    我是Son组件 {{ title }}
    <button @click="changeFn">修改title</button>
  </div>
</template>

<script>
export default {
  name: 'Son-Child',
  props: ['title'],
  methods: {
    changeFn() {
      //1. 通过this.$emit() 向父组件发送通知
      this.$emit('changTitle','Vue')
    },
  },
}
</script>

<style>
</style>
App.vue
<template>
  <div class="app" style="border: 3px solid #000; margin: 10px">
    我是APP组件
    <!-- 2.父组件对子组件的消息进行监听 -->
    <Son :title="myTitle" @changTitle="handleChange"></Son>
  </div>
</template>

<script>
import Son from './components/Son.vue'
export default {
  name: 'App',
  data() {
    return {
      myTitle: 'Hello Vue',
    }
  },
  components: {
    Son,
  },
  methods: {
    // 3.提供处理函数,提供逻辑
    handleChange(newTitle) {
      this.myTitle = newTitle
    },
  },
}
</script>

<style>
</style>

非父子关系 

provide&inject

eventbus

通用解决方案

Vuex(适合复杂业务场景)

Props

prop是组件上注册的一些自定义属性,用来向子组件传递数据

传值

可以传递任意数量、任意类型的prop

UserInfo.vue

<template>
  <div class="userinfo">
    <h3>我是个人信息组件</h3>
    <div>姓名:{{username}}</div>
    <div>年龄:{{age}}</div>
    <div>是否单身:{{isSingle}}</div>
    <div>座驾:{{car.brand}}</div>
    <div>兴趣爱好:{{hobby.join('、')}}</div>
  </div>
</template>

<script>
export default {
  props:['username','age','isSingle','car','hobby']
}
</script>

<style>
.userinfo {
  width: 300px;
  border: 3px solid #000;
  padding: 20px;
}
.userinfo > div {
  margin: 20px 10px;
}
</style>

App.vue

<template>
  <div class="app">
    <UserInfo
      :username="username"
      :age="age"
      :isSingle="isSingle"
      :car="car"
      :hobby="hobby"
    ></UserInfo>
  </div>
</template>

<script>
import UserInfo from './components/UserInfo.vue'
export default {
  data() {
    return {
      username: '小帅',
      age: 28,
      isSingle: true,
      car: {
        brand: '宝马',
      },
      hobby: ['篮球', '足球', '羽毛球'],
    }
  },
  components: {
    UserInfo,
  },
}
</script>

<style>
</style>

校验

作为组件的prop指定验证要求,不符合要求,控制台就会有错误提示

分为 类型校验、非空校验、默认值、自定义校验

语法:

类型校验

props:{

        校验的属性名:类型
}

类型、默认值、非空、自定义校验

props:{

        校验的属性名:{

                type:类型,

                required:true,

                default:默认值,

                validator(value){

                        //自定义校验逻辑

                        return true【false】

                }

        }

}

以下是百分比条案例

BaseProgress.vue

<template>
  <div class="base-progress">
    <div class="inner" :style="{ width: w + '%' }">
      <span>{{ w }}%</span>
    </div>
  </div>
</template>

<script>
export default {
  // 1.基础写法(类型校验)
  // props: {
  //   w: Number,
  // },

  // 2.完整写法(类型、默认值、非空、自定义校验)
  props: {
    w: {
      type: Number,
      required: true,
      default: 0,
      validator(val) {
        // console.log(val)
        if (val >= 100 || val <= 0) {
          console.error('传入的范围必须是0-100之间')
          return false
        } else {
          return true
        }
      },
    },
  },
}
</script>

<style scoped>
.base-progress {
  height: 26px;
  width: 400px;
  border-radius: 15px;
  background-color: #272425;
  border: 3px solid #272425;
  box-sizing: border-box;
  margin-bottom: 30px;
}
.inner {
  position: relative;
  background: #379bff;
  border-radius: 15px;
  height: 25px;
  box-sizing: border-box;
  left: -3px;
  top: -2px;
}
.inner span {
  position: absolute;
  right: 0;
  top: 26px;
}
</style>

App.vue

<template>
  <div class="app">
    <BaseProgress :w="width"></BaseProgress>
  </div>
</template>

<script>
import BaseProgress from './components/BaseProgress.vue'
export default {
  data() {
    return {
      width: 30,
    }
  },
  components: {
    BaseProgress,
  },
}
</script>

<style>
</style>

异同:data和props

 共同点:都可以给组件提供数据

区别:

  • data的数据是自己的,可以直接改
  • prop的数据是外部的,不能直接改,要遵循单项数据流(这里指父组件数据更新影响子组件,反之不然)

BaseCount.vue

<template>
  <div class="base-count">
    <button @click="handleSub">-</button>
    <span>{{ count }}</span>
    <button @click="handleAdd">+</button>
  </div>
</template>

<script>
export default {
  // 1.自己的数据随便修改  (谁的数据 谁负责)
  // data () {
  //   return {
  //     count: 100,
  //   }
  // },
  // 2.外部传过来的数据 不能随便修改
  props: {
    count: {
      type: Number,
    },
  },
  methods: {
    handleSub() {
      this.$emit('changeCount', this.count - 1)
    },
    handleAdd() {
      this.$emit('changeCount', this.count + 1)
    },
  },
}
</script>

<style>
.base-count {
  margin: 20px;
}
</style>

App.vue

<template>
  <div class="app">
    <BaseCount :count="count" @changeCount="handleChange"></BaseCount>
  </div>
</template>

<script>
import BaseCount from './components/BaseCount.vue'
export default {
  components:{
    BaseCount
  },
  data(){
    return {
      count:100
    }
  },
  methods:{
    handleChange(newVal){
      // console.log(newVal);
      this.count = newVal
    }
  }
}
</script>

<style>

</style>

相关推荐

  1. Vue通信

    2024-05-04 08:48:04       18 阅读
  2. vue通信

    2024-05-04 08:48:04       31 阅读
  3. vue 通讯

    2024-05-04 08:48:04       34 阅读
  4. vue通信的方式

    2024-05-04 08:48:04       39 阅读
  5. vue 之间通信的方式

    2024-05-04 08:48:04       45 阅读
  6. Vue通信的方式

    2024-05-04 08:48:04       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-04 08:48:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-04 08:48:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-04 08:48:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-04 08:48:04       20 阅读

热门阅读

  1. 前端初学者的 CSS 入门

    2024-05-04 08:48:04       12 阅读
  2. 蓝桥杯国赛备赛复习——数据结构

    2024-05-04 08:48:04       13 阅读
  3. 网络安全运维类面试非技术问题

    2024-05-04 08:48:04       10 阅读
  4. Python闭包:深入解析与使用场景

    2024-05-04 08:48:04       12 阅读
  5. helm安装 AWS Load Balancer Controller

    2024-05-04 08:48:04       14 阅读
  6. Apache Kafka知识点表格总结

    2024-05-04 08:48:04       11 阅读
  7. 什么是g++-arm-linux-gnueabihf

    2024-05-04 08:48:04       12 阅读
  8. Vue在/public目录下访问process.env.NODE_ENV(其它通用)

    2024-05-04 08:48:04       12 阅读
  9. Spark RDD

    2024-05-04 08:48:04       8 阅读
  10. git ,查看某个版本的某个文件内容

    2024-05-04 08:48:04       11 阅读