美文网首页
Vue 之 slot 插槽简单使用

Vue 之 slot 插槽简单使用

作者: 人话博客 | 来源:发表于2018-12-13 16:58 被阅读0次

Vue中定义组件,没什么大不了的.
我们可以定义一些组件.

比如 : 定义一个 panel 组件

Vue.component('panel',{
    template:'#panel-tpl',
    data() {
      return {
        title:'这是title',
        content:'Lorem ipsum dolor sit amet consectetur adipisicing elit. Ducimus pariatur ea molestiae alias, illum sed porro neque unde repudiandae. A, perspiciatis vel inventore unde modi reiciendis esse quasi commodi minima!',
        bottom:'查看详细'
      }
    }
  })
  
  
    <template id='panel-tpl'>
      <div class="panel">
          <div class="title">{{title}} </div>
          <div class="content"> {{content}}</div>
          <div class="bottom"> {{bottom}}</div>
        </div>
  </template>

长这模样.

15446899410400.jpg

Vue 中,一个组件内部可能会引用另外一个组件.也没什么大不了的.

比如,在定义一个图片组件.

 Vue.component('img-panel',{
    template:'<div><img src="./md/img/1.jpg" width="200px" height="auto" ></div>',
  })
  
  <img-panel></img-panel>

它长这样.

15446900991371.jpg

两个组件(panel & img-panel)都是全局组件.

我希望在 panel 组件里 div.content 里使用 img-panel 组件来填充.

我可能会这么写.

 <template id='panel-tpl'>
      <div class="panel">
          <div class="title">{{title}} </div>
          <div class="content"><img-panel></img-panel></div>
          <div class="bottom"> {{bottom}}</div>
        </div>
  </template>

两个组件强耦合了在一起,长这样:

15446910436003.jpg

现在问题来了

panel 组件的 div.content 这个部分,我是希望用内容填充的.
而不想管内容是什么.

但是这里的代码,我把 img-panel 组件写死到里面去了.
如果后期要换的话,我就得修改这里的代码.

 <template id='panel-tpl'>
      <div class="panel">
          <div class="title">{{title}} </div>
          <!-- <div class="content"><img-panel></img-panel></div> -->
          <!-- 不要 img-panel 了,还是显示自己的数据属性叭!!!! -->
          <div class="content">{{content}}</div>
          <div class="bottom"> {{bottom}}</div>
        </div>
  </template>

slot 插槽

slot 插槽可以在组件的内容预留一个位置.类似于 placeholder.

在需要的使用,把你需要的任何内容填充进去.

于是,我在 panel里放几个 slot ,只管挖坑,不管填坑(但设置了默认值)

  <template id='panel-tpl'>
      <div class="panel">
          <div class="title">
            <slot name='title'>{{title}}</slot>
          </div>
          <div class="content">
            <slot name='content'>{{content}}</slot>
          </div>
          <div class="bottom">
            <slot name='bottom'> {{bottom}}</slot>
          </div>
        </div>
  </template>
  
  
  
  
  Vue.component('title-slot',{
    template:`<div style='backgroundColor:red;height:40px'></div>`
  })

  Vue.component('content-slot',{
    template:`<div style='background:yellow;height:100px;'></div>`
  })
  Vue.component('bottom-slot',{
    template:`<div style='background:black;height:40px'></div>`
  })

然后在使用这个 panel 组件时,把需要的坑填进去.

<div id='app'>
   <panel>
    <div slot='title'><title-slot></title-slot></div>
    <div slot='content'><content-slot></content-slot></div>
    <div slot='bottom'><bottom-slot></bottom-slot></div>
   </panel>
  </div>

注意:我这里修改的是panel组件使用的位置,而不是定义的位置.

效果如下:

15446909419580.jpg

简单总结:

  • 在组件内部任意地方使用 <slot name='value'></slot> 标签挖坑.
  • 可以在其内部放置任意数据当成是默认值.{{title}} {{content}} {{bottom}}
    • 挖坑语法(组件定义): <slot name='slotname'>默认值</slot>
    • 填坑语法(组件使用): <div slot='slotname'>任意内容(字符串,数据,其他组件等)</div> .. 注意,使用的使用是里用 <div slot> 而不是 <slot>
  • 最重要的是,我们可以利用slot,事先不建立组件和组件之间的依赖关系,完全利用 slot 这个抽象层,把坑挖好即可.

相关文章

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • vue 插槽的使用

    vue 插槽手册 深入理解vue中的slot与slot-scope 插槽的使用其实是很简单首先要明白插槽是使用在子...

  • Vue之深入理解插槽—slot, slot-scope, v-s

    Vue 2.6.0 以前Vue 2.6.0 以后具名插槽 slot具名插槽 v-slot作用域插槽 slot-sc...

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

  • Vue 之 slot 插槽简单使用

    在Vue中定义组件,没什么大不了的.我们可以定义一些组件. 比如 : 定义一个 panel 组件 长这模样. 在 ...

  • vue3-slot-消息框-模态框

    1.前言 1.使用vue3 的slot插槽时,大部分和vue2-slot插槽[https://www.jiansh...

  • slot是什么?有什么作用?原理是什么?

    slot又名插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • slot(插槽)

    slot又称插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • vue-slot

    从官网上我们可以获知,Vue中的slot主要分为:单个插槽、具名插槽和作用域插槽三种。而其使用也较为简单,比如我们...

  • vue中的slot(插槽)

    vue中的插槽————slot 什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定...

网友评论

      本文标题:Vue 之 slot 插槽简单使用

      本文链接:https://www.haomeiwen.com/subject/okybhqtx.html