【Vue】快乐学习第四篇,组件访问及插槽使用

简介: 欢迎来到快乐学习Vue,组件访问及插槽的使用,快来学习吧 🏄🏼

文章目录


引出


父访问子$children


父访问子$refs


子访问父$parent


访问跟组件$root


插槽 slot


引出



有时候我们需要父组件直接访问子组件,子组件直接访问父组件,或者是子组件访问跟组件等等……这时候我们可以使用


🥣 父组件访问子组件:使用$children或$refs

🥣 子组件访问父组件:使用$parent


父访问子$children


9db94eedca2c4211815902e3791335d8.png


使用 $children 可以访问到子组件对象,如果有多个会返回一个数组对象,我们可以通过数组下标控制访问子组件的属性,方法等等


<body>
<div id="app">
    <zi></zi>
    <zi></zi>
    <button @click="getChildren">点击</button>
</div>
<template id="zi">
    <div>
        我是子组件
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            message: '你好'
        },
        methods: {
            getChildren() {
                console.log(this.$children)
                this.$children[0].showMessage()
                for (let i of this.$children) {
                    console.log(i.name)
                }
            }
        },
        components: {
            zi: {
                template: '#zi',
                data() {
                    return {
                        name: "小明·1"
                    }
                },
                methods: {
                    showMessage() {
                        console.log("showMessage 拿到了")
                    }
                }
            }
        }
    })
</script>
</body>


实际开发中,使用$children的次数可能并不多,原因很简单,实际业务复杂多变,对于使用数组下标的方式访问子组件,一旦子组件增加或删除,会直接导致数组下标的移位,导致bug出现,所以还有一种方式,比较推荐,就是使用 $ref


父访问子$refs


55cabd19e2d646359ed6808410a74e2b.png


在组件上使用ref属性给组件标识,再通过 $ref 获取属性值,就可以获取相应的组件对象,就不会出现使用下标获取导致的问题


<body>
<div id="app">
    <hello ref="a"></hello>
    <tip ref="b"></tip>
    <button @click="showMessage">获取组件message</button>
</div>
<template id="hello">
    <div>
        我是Hello组件
    </div>
</template>
<template id="tip">
    <div>
        我是tip组件
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        methods: {
            showMessage() {
                console.log(this.$refs)
                console.log('hello组件message: ' + this.$refs.a.message)
                console.log('tip组件message: ' + this.$refs.b.message)
            }
        },
        components: {
            hello: {
                template: '#hello',
                data() {
                    return {
                        message: 'Hello组件的message数据'
                    }
                }
            },
            tip: {
                template: '#tip',
                data() {
                    return {
                        message: 'Tip组件的message数据'
                    }
                }
            }
        }
    })
</script>
</body>


子访问父$parent


b39ad4a11c344b30b1780e1c97d1c273.png


使用$parent可以访问父组件,但是并不推荐这样使用,因为组件化开发的核心,就是拆分组件,复用组件,这样做有耦合,不利于组件的复用


<body>
<div id="app">
    <hello></hello>
</div>
<template id="hello">
    <div>
        <p>我是Hello组件是Vue实例的子组件</p>
        <button @click="getMessage">点击获取父组件的message</button>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            message: 'Vue实例:Hello World'
        },
        components: {
            hello: {
                template: '#hello',
                methods: {
                    getMessage() {
                        console.log(this.$parent)
                        console.log(this.$parent.message)
                    }
                }
            }
        }
    })
</script>
</body>


访问跟组件$root


e2564f1bc33f4aaba3105d7ce7afd3e4.png


使用$root可以访问跟组件


<body>
<div id="app">
    <hello></hello>
</div>
<template id="hello">
    <div>
        <p>我是Hello组件是Vue实例的子组件</p>
        <button @click="getMessage">点击获取根组件的message</button>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            message: 'Vue实例:Hello World'
        },
        components: {
            hello: {
                template: '#hello',
                methods: {
                    getMessage() {
                        console.log(this.$root)
                        console.log(this.$root.message)
                    }
                }
            }
        }
    })
</script>
</body>


插槽 slot



组件的插槽:

组件的插槽是为了让我们封装的组件更加具有扩展性

让使用者可以决定组件内部的一些内容到底显示什么


基本使用


<body>
<div id="app">
    <hello>
        <button>我是插槽里的按钮</button>
        <p>我在插槽里放p</p>
    </hello>
    <hr>
    <hello></hello>
</div>
<template id="hello">
    <div>
        <h3>我是老六</h3>
        <p>我是hello组件,嘿嘿🤭</p>
        <slot></slot>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        components: {
            hello: {
                template: '#hello'
            }
        }
    })
</script>
</body>

dce2980fe6534c06a20c4d1cdfec3270.png


具名插槽



给插槽起名,便于多个插槽替换时能定位

<body>
<div id="app">
    <hello>
        <button slot="3">wo</button>
        <span slot="2">嘿嘿</span>
    </hello>
</div>
<template id="hello">
    <div>
        <h3>我是老六</h3>
        <p>我是hello组件,嘿嘿🤭</p>
        <slot name="1">
            <button>我是插槽里的按钮1</button>
        </slot>
        <slot name="2">
            <button>我是插槽里的按钮2</button>
        </slot>
        <slot name="3">
            <button>我是插槽里的按钮3</button>
        </slot>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        components: {
            hello: {
                template: '#hello'
            }
        }
    })
</script>
</body>

7b7fa9f90c5946e09595000a31933c54.png


作用域插槽



父组件替换插槽的标签,但是内容由子组件来提供


<body>
<div id="app">
    <hello></hello>
    <hr>
    <hello>
        <span>java-c</span>
    </hello>
    <hr>
    <hello>
        <div slot-scope="slot">
            <span v-for="item in slot.data">{{item}} - </span>
        </div>
    </hello>
</div>
<template id="hello">
    <div>
        <slot :data="pLanguages">
            <ul>
                <li v-for="item in pLanguages">{{item}}</li>
            </ul>
        </slot>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        components: {
            hello: {
                template: '#hello',
                data() {
                    return {
                        pLanguages: ['JavaScript', 'C++', 'Java', 'Python', 'Go']
                    }
                }
            }
        }
    })
</script>
</body>


264df1d2316d4c72b8c84168b3580bd6.png

相关文章
|
2天前
|
JavaScript
|
1天前
|
存储 缓存 JavaScript
vue代码优化方案
【7月更文挑战第13天】 **Vue.js 优化要点:** 分解大组件以提高复用性和加载速度;利用计算属性与侦听器优化数据处理;使用Object.freeze()减少响应式数据;借助Vuex或Composition API管理状态;实现虚拟滚动和无限加载提升长列表性能;路由懒加载减少初始加载时间;用Vue DevTools检测性能瓶颈;定期代码审查与重构;应用缓存策略;遵循最佳实践与团队规范,提升应用整体质量。
10 2
|
4天前
|
JavaScript 前端开发
【vue】 el-table解决分页不能筛选全部数据的问题
【vue】 el-table解决分页不能筛选全部数据的问题
15 4
|
4天前
|
JavaScript
【vue】 vue2 监听滚动条滚动事件
【vue】 vue2 监听滚动条滚动事件
10 1
|
JavaScript
初识 Vue(19)---(Vue 中使用插槽(slot))
Vue 中使用插槽(slot) 案例:子组件中的一部分内容是根据父组件传递来的 DOM 来进行显示 Vue 中使用插槽(slot) Vue.
1241 0
|
4天前
|
JavaScript
【vue】el-dialog 内的tinymce弹窗被遮挡的解决办法 及 tinymce打开弹出菜单后直接关闭对话组件,导致该弹出菜单残留
【vue】el-dialog 内的tinymce弹窗被遮挡的解决办法 及 tinymce打开弹出菜单后直接关闭对话组件,导致该弹出菜单残留
16 6
|
4天前
|
JavaScript 定位技术
【天地图】vue 天地图 T is not defined
【天地图】vue 天地图 T is not defined
15 1
|
9天前
|
数据采集 JavaScript 前端开发
Vue框架的优缺点是什么
【7月更文挑战第5天】 Vue框架:组件化开发利于重用与扩展,响应式数据绑定简化状态管理;学习曲线平缓,生态系统丰富,集成便捷,且具性能优化手段。缺点包括社区规模相对小,类型支持不足(Vue 3.x改善),路由和状态管理需额外配置,SEO支持有限。随着发展,部分缺点正被克服。
19 1
|
9天前
|
JavaScript
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
|
4天前
|
JavaScript 前端开发
【vue】 Tinymce 数据 回显问题 | 第一次正常回显后面,显示空白bug不能编辑
【vue】 Tinymce 数据 回显问题 | 第一次正常回显后面,显示空白bug不能编辑
11 0