Vue前端框架11 组件事件与v-mode配合使用、组件数据传递(父传子)、插槽Slot、具名插槽、插槽中的数据传递(双向)

文章目录

一、组件事件与v-model配合使用

组件A的数据变化

组件B可以实时显示

javascript 复制代码
<template>
  <h3>Main</h3>
  <p>搜索内容为:{{search}}</p>
  <component-b @searchEvent="getSearch"></component-b>
</template>

<script>
import ComponentB from "@/components/componentB";
export default {
  name: "componentA",
  components: {ComponentB},
  data(){
    return{
      search:""
    }
  },
  methods:{
    getSearch(data){
      this.search=data
    }
  }
}
</script>

<style scoped>

</style>
javascript 复制代码
<template>
  搜索: <input type="text" v-model="search">
</template>

<script>
export default {
  name: "componentB",
  data(){
    return{
      search:""
    }
  },
  //侦听器
  watch:{
    search(newvValue,oldValue){
      console.log(oldValue)
      this.$emit("searchEvent",newvValue)
    }
  }
}
</script>

<style scoped>

</style>

二、组件数据传递(子传父)

利用父组件的函数回调

javascript 复制代码
<template>
  <h3>Main</h3>
  <p>搜索内容为:{{search}}</p>
  <component-b @searchEvent="getSearch" :onEvent="dataFn"></component-b>
  <p>this:{{message}}</p>
</template>

<script>
import ComponentB from "@/components/componentB";
export default {
  name: "componentA",
  components: {ComponentB},
  data(){
    return{
      search:"",
      message:""
    }
  },
  methods:{
    getSearch(data){
      this.search=data
    },
    dataFn(data){
      this.message=data
    }
  }
}
</script>

<style scoped>

</style>
javascript 复制代码
<template>
  搜索: <input type="text" v-model="search">
  <p>{{onEvent(age)}}</p>
</template>

<script>
export default {
  name: "componentB",
  data(){
    return{
      search:"",
      age:22
    }
  },
  //侦听器
  watch:{
    search(newvValue,oldValue){
      console.log(oldValue)
      this.$emit("searchEvent",newvValue)
    }
  },props:{
    onEvent:Function
  }
}
</script>

<style scoped>

</style>

三、插槽Slots

解决的是组件传输一个完整的html结构 也就是一个模板内容

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
    <div>
      <h3>插槽标题</h3>
<!--      插槽内容不是写死的 而是动态的-->
      <p>{{ message }}</p>
    </div>
  </slot-demo>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
export default {
  name: 'App',
  components: {SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello"
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
javascript 复制代码
<template>
  <h3>插槽基础知识</h3>
<!--  slot 是一个插槽的出口 标示了 父元素提供的插槽内容将要在哪里被渲染 -->
<!--  如果没有传插槽 在标签中间的内容 就是默认值-->
  <slot>插槽默认值</slot>
</template>

<script>
export default {
  name: "slotDemo"
}
</script>

<style scoped>

</style>

四、具名插槽

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
<!--   通过template中的 v-slot属性 和 slot标签的 name对应 -->
<!--    v-slot的简写 可以 简写为# -->
    <template v-slot:header>
      <h3>插槽标题</h3>
    </template>
    <template #main>
      <p>{{ message }}</p>
    </template>

  </slot-demo>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
export default {
  name: 'App',
  components: {SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello"
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
javascript 复制代码
<template>
  <h3>插槽基础知识</h3>
<!--  slot 是一个插槽的出口 标示了 父元素提供的插槽内容将要在哪里被渲染 -->
<!--  如果没有传插槽 在标签中间的内容 就是默认值-->
  <slot name="header">插槽默认值</slot>
  <hr>
  <slot name="main"></slot>
</template>

<script>
export default {
  name: "slotDemo"
}
</script>

<style scoped>

</style>

五、插槽中的数据传递

在某些情况下,插槽的内容可能同时需要用到父子两个组件的数据,所以需要方法将子组件在渲染时候将一部分数据提供给插槽

javascript 复制代码
<template>
  <parent-demo></parent-demo>
  <component-a></component-a>
  <slot-demo>
<!--   通过template中的 v-slot属性 和 slot标签的 name对应 -->
<!--    v-slot的简写 可以 简写为# -->
    <template v-slot:header>
      <h3>插槽标题</h3>
    </template>
    <template #main>
      <p>{{ message }}</p>
    </template>
  </slot-demo>

  <SlotAttr>
<!--    建议指明对应的插槽名称-->
    <template v-slot:main="slotProps">
      <p>{{currentTest}}</p>
      <p>{{slotProps.msg}}</p>
    </template>
  </SlotAttr>
</template>

<script>


import ParentDemo from "@/components/parentDemo";
import ComponentA from "@/components/componentA";
import SlotDemo from "@/components/slotDemo";
import SlotAttr from "@/components/slotAttr";
export default {
  name: 'App',
  components: {SlotAttr, SlotDemo, ComponentA, ParentDemo},
  data(){
    return{
      message:"hello",
      currentTest:"测试内容"
    }
  }
}
</script>
javascript 复制代码
<template>
  <h3>插槽plus</h3>
<!--  子元素的数据想要显示 则需要将数据传给父元素
      父元素接收后和父元素原本的内容 一起传给子元素
-->
  <slot :msg="childMessage" name="main">

  </slot>
</template>

<script>
export default {
  name: "slotAttr",
  data(){
    return{
      childMessage:"子组件数据"
    }
  }
}
</script>

<style scoped>

</style>
相关推荐
不爱吃饭爱吃菜17 分钟前
uniapp微信小程序一键授权登录
前端·javascript·vue.js·微信小程序·uni-app
90后小陈老师1 小时前
3D个人简历网站 5.天空、鸟、飞机
前端·javascript·3d
不爱吃糖的程序媛5 小时前
浅谈前端架构设计与工程化
前端·前端架构设计
BillKu6 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
郝YH是人间理想7 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core7 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情7 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287568 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔8 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好8 小时前
HTML 表格与div深度解析区别及常见误区
前端·html