一、插槽的理解

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理解与初体验 ~的更多相关文章

  1. vue.js2.0 自定义组件初体验

    理解 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在有些情况 ...

  2. vue 插槽slot总结 slot看这篇就够了

    一直模糊所以梳理一下,看了好多篇园友的文章和官网文档在这整理一下 默认插槽 //slot组件<template> <div class="slots"> s ...

  3. vue 插槽slot

    本文是对官网内容的整理 https://cn.vuejs.org/v2/guide/components.html#编译作用域 在使用组件时,我们常常要像这样组合它们: <app> < ...

  4. Vue插槽 slot

    1. 什么是插槽 插槽slot 是往父组件中插入额外内容,实现组件的复用,一个插槽插入到一个对应的标签中 2. 实例: 一个组件中不允许有两个匿名插槽 </head> <body&g ...

  5. 三、深入Vue组件——Vue插槽slot、动态组件

    一.插槽slot() 1.1简单插槽slot [功能]用于从父组件中,通过子组件写成双标签,向子组件中放入自定的内容 parent.vue [1]首先把child写成双标签样式,把要插入的内容放双标签 ...

  6. vue插槽slot的理解与使用

    一.个人理解及插槽的使用场景 刚开始看教程我的疑惑是为什么要用插槽,它的使用场景是什么,很多解释都是“父组件向子组件传递dom时会用到插槽”,这并不能很好的解决我的疑惑.既然你用了子组件,你为什么要给 ...

  7. vue 插槽 ------ slot 简单理解

    solt 插槽 内容分发 什么是插槽 Vue 实现了一套内容分发的 API,将 `` 元素作为承载分发内容的出口. 插槽显示的位置却由子组件自身决定,槽写在组件模板的什么位置,父组件传过来的模板将来就 ...

  8. vue中的插槽slot理解

    本篇文章参考赛冷思的个人博客 1.函数默认传参 在我们写js函数我们的可能会给他们一个默认的参数,写法是 function show(age,name){ var age = age || 20; v ...

  9. vue 插槽 slot

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. C# Fakes

    我们为了测试程序的运行逻辑,需要写单元测试来验证程序的逻辑.有的时候我们的逻辑需要依赖于外界的事物(需要一个文件,eg:数据库),我们不可能在运行单元测试的计算机都创建一个数据库,所以这个时候我们就需 ...

  2. Spring PropertyPlaceholderConfigurer 自定义扩展

    原文地址:https://blog.csdn.net/feiyu8607/article/details/8282893 Spring中PropertyPlaceholderConfigurer这个类 ...

  3. Difference between trustStore and keyStore in Java - SSL

    Difference between trustStore and keyStore in Java - SSL   trustStore vs keyStore in Java trustStore ...

  4. ObjectInputStream和ObjectOutputStream

    package stream.object; import java.io.FileInputStream; import java.io.FileOutputStream; import java. ...

  5. flink双流join

    package com.streamingjoin import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor ...

  6. 高德地图——2D转换3D

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script ty ...

  7. Python之uiautomation模块-获取CMD窗口中所打印的文字信息

    当我们想以自动化的方式操作软件,以提高办公或测试效率时,有许多成熟的工具,比如针对Web端应用的Selenium.针对移动端应用的Appium.那么,PC端(Windows)桌面应用,又改如何处理呢? ...

  8. vue.js框架图片上传组件

    html: <div id="app"> <div class="hello"> <div class="upload& ...

  9. 对easyui-validatebox的验证类型的扩展--补充

    一.说明 这篇文章是<对easyui-validatebox的验证类型的扩展>的补充.在工程的持续开发中,我们又对此进行了更多的补充. 二.补充代码 增加了更多的验证类型. /* * 比较 ...

  10. 谈谈Linux系统启动流程

    @ 目录 大体流程分析 一.BIOS 1.1 BIOS简介 1.2 POST 二.BootLoader (GRUB) 2.1 What's MBR? 2.2 What's GRUB? 2.3 boot ...