Vue插槽slot理解与初体验 ~
一、插槽的理解
1.官网介绍
Vue 实现了一套内容分发的 API,将 <slot> 元素作为承载分发内容的出口。
2.为什么使用插槽

Vue 中有一个重要的概念-组件,可以在开发中将子组件插入到父组件中,因此需要给子组件组件留出位置(这里的组件我的理解是可以理解成sql的一个占位符.),如图slot提供可以插入的位置,我们将component1和component2插入到big component中。

二、使用步骤
1.希望最终得到的页面
<div id="app">
<div>
<h3>图书列表</h3>
<ul>
<li>红楼梦</li>
</ul>
</div>
</div>

可以将该页面分为三部分来看,将这三部分注册成vue的组件
2.组件注册
<div id="app">
<book-component></book-component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
<script>
//图书组件
Vue.component('book-component',{
template: '<div><h3>图书列表</h3><ul><li>红楼梦</li></ul></div>'
});
//图书标题组件
Vue.component('book-component-title',{
template: '<h3>图书列表</h3>'
});
//图书列表组件
Vue.component('book-component-list',{
template: '<li>红楼梦</li>'
});
let vApp = new Vue({
el: '#app'
});
</script>
直接引入注册的组件就能实现列表展示,下面需要把子组件插入到父组件中
3.添加插槽
<div id="app">
<book-component>
<book-component-title slot="title"></book-component-title>
<book-component-list slot="list"></book-component-list>
</book-component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
<script>
//图书组件
Vue.component('book-component',{
//<div>
// <slot name="title"></slot>
// <slot name="list"></slot>
//</div>
template: '<div><slot name=\'title\'></slot><ul><slot name=\'list\'></slot></ul></div>'
});
//图书标题组件
Vue.component('book-component-title',{
template: '<h3>图书列表</h3>'
});
//图书列表组件
Vue.component('book-component-list',{
template: '<li>红楼梦</li>'
});
let vApp = new Vue({
el: '#app'
});
</script>
在父组件中加入slot="",父组件slot中name可以随意指定;在子组件中加入<slot> 标签,标签的name必须和父组件对应

vue3.0后,v-slot:插槽名 取代了slot="插槽名"的写法 可参考:https://www.cnblogs.com/LUA123/p/10812164.html
3.绑定数据并传递
<div id="app">
<book-component>
<book-component-title slot="title" v-bind:ti="title"></book-component-title>
<book-component-list slot="list" v-for="li in list" v-bind:l="li"></book-component-list>
</book-component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
<script>
//图书组件
Vue.component('book-component',{
template: '<div><slot name=\'title\'></slot><ul><slot name=\'list\'></slot></ul></div>'
});
//图书标题组件
Vue.component('book-component-title',{
props: ['ti'],
template: '<h3>{{ti}}</h3>'
});
//图书列表组件
Vue.component('book-component-list',{
props: ['l'],
template: '<li>{{l}}</li>'
});
let vApp = new Vue({
el: '#app'
,data: {
title: '图书列表'
,list: [
'红楼梦','三国演义','水浒传','西游记'
]
}
});
</script>
通过'props'接收参数,参数对应关系不要记错就行。

绑定数据之后最终页面

Vue插槽slot理解与初体验 ~的更多相关文章
- vue.js2.0 自定义组件初体验
理解 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在有些情况 ...
- vue 插槽slot总结 slot看这篇就够了
一直模糊所以梳理一下,看了好多篇园友的文章和官网文档在这整理一下 默认插槽 //slot组件<template> <div class="slots"> s ...
- vue 插槽slot
本文是对官网内容的整理 https://cn.vuejs.org/v2/guide/components.html#编译作用域 在使用组件时,我们常常要像这样组合它们: <app> < ...
- Vue插槽 slot
1. 什么是插槽 插槽slot 是往父组件中插入额外内容,实现组件的复用,一个插槽插入到一个对应的标签中 2. 实例: 一个组件中不允许有两个匿名插槽 </head> <body&g ...
- 三、深入Vue组件——Vue插槽slot、动态组件
一.插槽slot() 1.1简单插槽slot [功能]用于从父组件中,通过子组件写成双标签,向子组件中放入自定的内容 parent.vue [1]首先把child写成双标签样式,把要插入的内容放双标签 ...
- vue插槽slot的理解与使用
一.个人理解及插槽的使用场景 刚开始看教程我的疑惑是为什么要用插槽,它的使用场景是什么,很多解释都是“父组件向子组件传递dom时会用到插槽”,这并不能很好的解决我的疑惑.既然你用了子组件,你为什么要给 ...
- vue 插槽 ------ slot 简单理解
solt 插槽 内容分发 什么是插槽 Vue 实现了一套内容分发的 API,将 `` 元素作为承载分发内容的出口. 插槽显示的位置却由子组件自身决定,槽写在组件模板的什么位置,父组件传过来的模板将来就 ...
- vue中的插槽slot理解
本篇文章参考赛冷思的个人博客 1.函数默认传参 在我们写js函数我们的可能会给他们一个默认的参数,写法是 function show(age,name){ var age = age || 20; v ...
- vue 插槽 slot
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
随机推荐
- WPF日积月累之TreeView动态绑定
一.概述 本文演示了如何递归生成数据,用于绑定TreeView以及TreeItem的双击事件. 二.参考代码 1 using System; 2 using System.Collections.Ge ...
- css - 行高
css - 行高 line-height行高 取值:px | em | rem | 百分比 | 纯数字 | normal | inherit 设置给:块.行内.行内块 应用给:文本 继承:块.行内.被 ...
- GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
1 drop table orders; 2 create table orders ( 3 o_id int auto_increment primary key, 4 orderdate date ...
- 管理 Python 多版本,pyenv 用起来
介绍 学习使用pyenv在本地安装多个 Python 版本,这样既不影响工作,也不影响生活~ pyenv 可让你轻松地在多个 Python 版本之间切换.它简单.不引人注目,并且遵循 UNIX 的单一 ...
- 详细解读go语言中的map
Map map底层是由哈希表实现的 Go使用链地址法来解决键冲突. map本质上是一个指针,指向hmap 这里的buckets就是桶,bmap 每一个bucket最多可以放8个键值对,但是为了让内存排 ...
- 一个基于activiti审批流程示例,如何与系统整合
前言 目前市场上有很多开源平台没有整合工作流,即使有,也是价格不菲的商业版,来看这篇文章的估计也了解了行情,肯定不便宜.我这个快速开发平台在系统基础功能(用户管理,部门管理-)上整合了工作流,你可以直 ...
- 剑指 Offer 36. 二叉搜索树与双向链表
剑指 Offer 36. 二叉搜索树与双向链表 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 为了让您更好地理解问题,以下面的 ...
- Robot framework随机文件
*** Variables *** @{Example} One Two Three *** Test Cases *** Example ${value}= Evaluate random.choi ...
- docker-harbor私有仓库使用笔记
1. 登录harbor管理页面,创建项目,比如yuqx_test 2. admin登录,此处免密登录,正常情况下会输入账号密码 [root@k8s-rancher2 ~]# docker login ...
- python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))
python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) 问题: ...