vue 中的 $slot 获取 插槽的节点
2018-03-12 16:08
1621 查看
vue 中的 $slot
以前一直不知到这个东西,后来发现 vue api 中 藏着很多的 很神奇的 api,比如这个
具名插槽很好理解,但是那个 default 就有点难了,
写了一个炒鸡简单的 demo
father:
<template> <div> <button @click="getSlot">getSlot</button> <try ref="try"> <div class="hello1">hello1</div> <div class="hello2">hello2</div> <div class="hello3">hello3</div> </try> <button @click="getArc">getArc</button> </div></template><script>import try from './try'export default { components: { try }, methods: { getSlot () { this.$refs.try.getSlot() } }}</script>try.vue
<template> <div> <h2>我是子组件 的 标题</h2> <slot> 只有在没有内容分发的时候我才会出现 </slot> </div></template><script>export default { methods: { getSlot () { console.log(this.$slots) } }}</script>
点击了getSlot 之后的输出
可以看到 default ,
里面有插入的 三个 标签和 三个标签之间的 两个 空格,就有 5 个 了
通过这个就能很轻易的 拿到 父组件 通过插槽插入 子组件的 标签了
this.slotChildren = [] for (let i = 0; i< this.$slots.default.length; i++) { if (that.$slots.default[i].elm.nodeType !== 3) { that.slotChildren.push(that.$slots.default[i]) // 获得 那些 插入的 按钮 } }
以前一直不知到这个东西,后来发现 vue api 中 藏着很多的 很神奇的 api,比如这个
具名插槽很好理解,但是那个 default 就有点难了,
写了一个炒鸡简单的 demo
father:
<template> <div> <button @click="getSlot">getSlot</button> <try ref="try"> <div class="hello1">hello1</div> <div class="hello2">hello2</div> <div class="hello3">hello3</div> </try> <button @click="getArc">getArc</button> </div></template><script>import try from './try'export default { components: { try }, methods: { getSlot () { this.$refs.try.getSlot() } }}</script>try.vue
<template> <div> <h2>我是子组件 的 标题</h2> <slot> 只有在没有内容分发的时候我才会出现 </slot> </div></template><script>export default { methods: { getSlot () { console.log(this.$slots) } }}</script>
点击了getSlot 之后的输出
可以看到 default ,
里面有插入的 三个 标签和 三个标签之间的 两个 空格,就有 5 个 了
通过这个就能很轻易的 拿到 父组件 通过插槽插入 子组件的 标签了
this.slotChildren = [] for (let i = 0; i< this.$slots.default.length; i++) { if (that.$slots.default[i].elm.nodeType !== 3) { that.slotChildren.push(that.$slots.default[i]) // 获得 那些 插入的 按钮 } }
相关文章推荐
- vue slot插槽的小技巧
- Vue的基础认知二---vue的双向绑定/vue获取DOM节点
- vue.js之slot插槽
- vue中的作用域插槽--理解(slot-scope)
- Vue——Slot(插槽)
- vue elementUI tree树形控件获取父节点ID的实例
- 详解vue slot插槽的使用方法
- Vue插槽--解决点击整体而不是局部获取id
- Vue中的slot使用插槽分发内容的方法
- vue---vue中如何操作dom获取节点?.vue组件中通过mounted获取节点操作
- SpringBoot获取Redis集群实例有缺少的问题->为Redis集群新增节点分配slot
- vue踩坑:slot插槽,vue路由传值
- Vue(router,导航守卫,slot插槽)
- vue动态组件和slot插槽
- vue 插槽slot
- vue使用插槽分发内容slot的用法
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件
- vue slot插槽的使用方法
- vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由
- vue中的slot(插槽)