当前位置: 首页 > news >正文

vue的插槽解析

插槽

好处:组件的内容结构可定制 用slot插槽进行占位

语法: 子组件中通过slot进行占位

理解:父组件,在子组件标签嵌套的内容就会被渲染到slot地方

一、默认插槽

//子组件
<slot>slot插槽</slot> //方法一<slot name="default">slot插槽</slot> //方法二
//父组件
<exercise-first>slot插槽</exercise-first>

二、具名插槽

//子组件
<slot name="header"></slot>

通过指定 name 属性来定义一个具名插槽。在使用组件时,可以将内容分发到对应的具名插槽中。

//父组件  v-slot:main
<exercise-first><template v-slot:main><div>具名组件</div></template>
</exercise-first>

 注意:

一、通过v-slot:来查找具名插槽

二、为什么用template, template等于是一个包,不是div盒子,页面是看不到template标签的

三、作用域插槽

作用域插槽的语法: v-slot:slotName="parameter",其中 slotName 是插槽的名称,parameter 是接收子组件数据的参数。

接收:的是已 对象的形式

//父组件中定义的
<template v-slot:slotname2="obj">
//子组件中定义的
<slot name="slotname2" :item1='item' value="111" ></slot>

案例:

<!-- 子组件 -->
<template><div><slot name="item" v-for="item in items" :item="item"></slot></div>
</template><!-- 父组件 -->
<template><div><template v-slot:item="{ item }"><p>{{ item }}</p></template></div>
</template>

四、具名插槽缩写写法

v-slot缩写#

v-onv-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header

   <template v-slot:dalete></template>等同于:<template #dalete></template>
<slot name="header"></slot>
<!-- 缩写写法 -->
<slot name="header"/>

http://www.lryc.cn/news/266309.html

相关文章:

  • Spring(3)Spring从零到入门 - Spring整合技术及AOP事务管理
  • 适配器模式学习
  • NET中使用Identity+CodeFirst+Jwt实现登录、鉴权
  • 详解Keras3.0 API: Optimizers
  • 【数据结构】字符串匹配|BF算法|KMP算法|next数组的优化
  • 阿里云 ACK One 新特性:多集群网关,帮您快速构建同城容灾系统
  • vscode自定义代码片段
  • 【贪心算法】专题练习一
  • 【JMeter】使用nmon进行性能资源监控
  • Unity预设体
  • Elasticsearch 写入优化探索:是什么影响了refresh 耗时?
  • Java8新特性——函数式接口
  • Epson打印机连接wifi
  • Chapter 7 - 6. Congestion Management in Ethernet Storage Networks以太网存储网络的拥塞管理
  • 【论文笔记】NeuRAD: Neural Rendering for Autonomous Driving
  • 通信原理 | 分贝dB、功率、功率谱、功率谱密度、信噪比
  • Go中的Context是什么?
  • 碳排放预测 | 基于ARIMA和GM(1,1)的碳排放预测(Matlab)
  • FPFA.一种二倍频电路代码描述以及测量详情
  • dotnet命令创建C#项目,VSCode打开
  • 在GitHub找开源项目
  • GAMES101-LAB1
  • Docker 编译OpenHarmony 4.0 release
  • Vue 3 表单处理精讲:打造响应式注册表单的艺术
  • 浅谈Guava Cache的参数使用
  • 交通流预测 | Matlab基于KNN-BiLSTM的交通流预测(对比SVR、LSTM、GRU、KNN-LSTM)
  • 云卷云舒:面向业务的智能运维(上)
  • centos 7.4 docker
  • 零基础学人工智能:TensorFlow 入门例子
  • go从0到1项目实战体系二一:gin框架安装