vue2知识点————(vue插槽,透传 Attributes )

news/2024/9/23 22:39:51/

vue 插槽

插槽(slot)是一种强大的特性,允许在组件的模板中定义带有特定用途的“插槽”,然后在组件的使用者中填充内容。插槽能够使组件更加灵活,让组件的结构更容易复用和定

具名插槽(Named Slots)

  • 具名插槽允许你在组件中定义多个插槽,并为每个插槽取一个名称。
  • 在使用组件时,可以使用 v-slot 指令来为具名插槽提供内容,并通过指定插槽的名称来匹配到对应的插槽。
javascript"><!-- ChildComponent.vue -->
<template><div><slot name="header"></slot><slot name="content"></slot></div>
</template><!-- ParentComponent.vue -->
<template><div><ChildComponent><template v-slot:header><h1>这是标题</h1></template><template v-slot:content><p>这是内容</p></template></ChildComponent></div>
</template>

作用域插槽(Scoped Slots)

  • 作用域插槽允许子组件向父组件传递数据。
  • 使用 v-slot 的缩写语法 #,可以在父组件的模板中访问子组件中的数据,并在父组件的作用域中使用这些数据。
javascript"><!-- ChildComponent.vue -->
<template><div><slot :message="message"></slot></div>
</template><script>
export default {data() {return {message: '这是子组件传递的消息'};}
};
</script><!-- ParentComponent.vue -->
<template><div><ChildComponent><template v-slot="{ message }"><p>{{ message }}</p></template></ChildComponent></div>
</template>

默认插槽(Default Slots)

  • 如果组件没有具名插槽,那么其所有内容都会被放入默认插槽中。
  • 默认插槽可以简化组件的使用,使其更加直观。
javascript"><!-- ChildComponent.vue -->
<template><div><slot></slot></div>
</template><!-- ParentComponent.vue -->
<template><div><ChildComponent><p>这段内容会放在默认插槽中</p></ChildComponent></div>
</template>

作用域插槽的使用场景

  • 当你需要在父组件中使用子组件的数据时,作用域插槽非常有用。
  • 作用域插槽使得组件的数据传递更加灵活,能够适应各种复杂的场景。
  1. 插槽的使用方法

    • 在组件的模板中,使用 <slot> 元素来定义插槽的位置。
    • 在组件的使用者中,使用 v-slot 或 # 来填充插槽。
  2. 动态插槽名

    • 插槽名可以是动态的,可以使用 JavaScript 表达式来指定插槽的名称。
    • 这样可以根据组件的状态或属性来动态决定插槽的内容。
  3. 作用域插槽的数据传递

    • 通过在子组件中使用 v-bind 将数据绑定到插槽上,可以将数据传递给父组件。
    • 父组件可以在使用插槽时,通过作用域插槽的参数来访问这些数据。

动态插槽名

javascript"><!-- ChildComponent.vue -->
<template><div><slot :name="slotName"></slot></div>
</template><script>
export default {data() {return {slotName: 'dynamicSlot'};}
};
</script><!-- ParentComponent.vue -->
<template><div><ChildComponent><template v-slot:[dynamicSlot]><p>这段内容会放在动态插槽中</p></template></ChildComponent></div>
</template>

透传 Attributes

 Attributes 是指组件的特性或属性,可以通过这些特性来配置组件的行为或样式。Attributes 可以是静态的,也可以是动态的,可以接受字符串、数字、布尔值等不同类型的值。Attributes 是组件的重要配置选项之一,用于与组件进行交互和通信。

静态 Attributes

  • 静态 Attributes 是在组件声明或使用时直接指定的,其值是固定的,不会随着组件状态的改变而改变。
javascript"><!-- MyComponent.vue -->
<template><div :class="className">组件内容</div>
</template><script>
export default {props: {className: String}
};
</script><!-- ParentComponent.vue -->
<template><div><MyComponent class="static-class"></MyComponent></div>
</template>

动态 Attributes

  • 动态 Attributes 是根据组件的状态或属性值动态地指定的,可以根据需要在运行时更改。
javascript"><!-- MyComponent.vue -->
<template><div :class="className">组件内容</div>
</template><script>
export default {props: {className: String},data() {return {dynamicClass: 'dynamic-class'};}
};
</script><!-- ParentComponent.vue -->
<template><div><MyComponent :class="dynamicClass"></MyComponent></div>
</template><script>
export default {data() {return {dynamicClass: 'another-dynamic-class'};}
};
</script>

绑定 Attributes

  • 除了绑定类名之外,还可以绑定其他 Attributes,如 idstyle 等。
javascript"><!-- MyComponent.vue -->
<template><div :id="id" :style="{ color: textColor }">组件内容</div>
</template><script>
export default {props: {id: String,textColor: String}
};
</script><!-- ParentComponent.vue -->
<template><div><MyComponent :id="componentId" :text-color="componentTextColor"></MyComponent></div>
</template><script>
export default {data() {return {componentId: 'component-id',componentTextColor: 'red'};}
};
</script>

特殊 Attributes:

  • Vue.js 2 中还有一些特殊的 Attributes,如 keyref 等,它们具有特殊的含义和用途,在特定场景下使用。
javascript"><!-- MyComponent.vue -->
<template><input :value="inputValue" @input="updateInput">
</template><script>
export default {data() {return {inputValue: ''};},methods: {updateInput(event) {this.inputValue = event.target.value;}}
};
</script>

http://www.ppmy.cn/news/1437954.html

相关文章

Java openrasp记录-01

例子1 https://github.com/anbai-inc/javaweb-expression 一个hook ognl、spel、MVEL表达式注入的例子 用的是asm5进行字节码修改 采用premain进行插桩&#xff0c;重写transform方法 expClassList是要hook的类&#xff0c;这里定义在MethodHookDesc 这里判断hook点通过类名…

02 贪吃蛇

前言 呵呵 这是不知道 在哪里看到的 别人做的一个贪吃蛇 因此 也把我 之前的 贪吃蛇 移植上来了 当然 这个不过是为了 简单的入门了解, 呵呵 然后 c版本的贪吃蛇 需要先移植成 c 版本, 然后 再根据 单片机相关 设计调整 比如 led 点阵的输出, 比如 c99 语法的一些不兼容…

1小时学会SpringBoot3+Vue3前后端分离开发

首发于Enaium的个人博客 引言 大家可能刚学会Java和Vue之后都会想下一步是什么&#xff1f;那么就先把SpringBoot和Vue结合起来&#xff0c;做一个前后端分离的项目吧。 准备工作 首先你需要懂得Java和Vue的基础知识&#xff0c;环境这里就不多说了&#xff0c;直接开始。 …

Kotlin作用域函数引发的遮蔽问题

前面讲了kotlin的it变量引起的遮蔽问题&#xff0c;见Kotlin it隐式变量的遮蔽问题&#xff0c;本篇聊聊作用域函数&#xff08;scoped function&#xff09;可能引起的遮蔽问题。 先来看一个简单的示例&#xff1a; fun test(): String {val s: String "asdf".al…

selenium显式等待

selenium显示等待 确保页面元素在特定条件下加载完毕后再执行后续操作。显示等待可以通过WebDriverWait类和ExpectedConditions类来实现。通过使用显示等待&#xff0c;可以避免在页面元素还未加载完毕时就进行操作&#xff0c;从而提高测试的稳定性和可靠性。 # 导包 from s…

leetcode543--二叉树的直径

1. 题意 求二叉树上最远两个节点之间的距离。 2. 题解 2.1 暴力 最长路径的三种情况 通过根节点在左子树在右子树 12 4 5 6 7 8 9 diameter 5通过根节点的最长路径长度一定是左右子树深度之和。 但是这样求左右子树的深度会不断重复&#xff0c;所以复杂度…

从虚拟化走向云原生,红帽OpenShift“一手托两家”

汽车行业已经迈入“软件定义汽车”的新时代。吉利汽车很清醒地意识到&#xff0c;只有通过云原生技术和数字化转型&#xff0c;才能巩固其作为中国领先汽车制造商的地位。 和很多传统企业一样&#xff0c;吉利汽车在走向云原生的过程中也经历了稳态业务与敏态业务并存带来的前所…

【docker】安装openjdk

查看可用的 openjdk版本 docker hub 查看地址&#xff1a;https://hub.docker.com/_/openjdk 此图片已被正式弃用&#xff0c;建议所有用户尽快找到并使用合适的替代品。其他官方形象替代品的一些例子&#xff08;按字母顺序列出&#xff0c;没有有意或暗示的偏好&#xff09;…