美文网首页
slot内容分发

slot内容分发

作者: 是素净呀丶 | 来源:发表于2017-04-26 10:00 被阅读0次

slot: 位置;狭槽;水沟;硬币投币口 ---from有道词典
个人理解为位置预占, 有新增则覆盖, 无则显示预占内容

一、 默认情况

<template>
  <div id='app'>
    <children>
      <span> i am tangerine </span>
    </children>
  </div>
</template>

<script>
   import Vue from 'vue';
   new Vue({
       components: {
         children: { template: '<p> i am children </p>' }
       }
   }).$mount('#app')
</script>

此时 <span> i am tangerine </span> 不会显示 由于children组件中并未设置插槽。

二、单个slot

<template>
 <div id='app'>
   <children>
     <span> i am tangerine </span>
   </children>
 </div>
</template>

<script>
  import Vue from 'vue';
  new Vue({
      components: {
        children: { template: '<p> i am children<slot><span> i am slot without name </span></slot> </p>' }
      }
  }).$mount('#app')
</script>

此时<span> i am tangerine </span>将替换<slot>整个标签进行渲染 如果无<span> i am tangerine </span>将会显示默认内容<span> i am slot without name </span>

二、具名slot

<template>
  <div id='app'>
    <children>
      <span slot="firstSlot"> i am tangerine </span>
      <span> who am i ? </span>
      <span slot="secondSlot"> i am apple </span>
      <span> who are you ? </span>
      <span slot="firstSlot"> i am orange </span>
    </children>
  </div>
</template>

<script>
   import Vue from 'vue';
   new Vue({
       components: {
         children: { template: '<p> i am children<slot name="firstSlot"><span> i am slot one </span></slot><slot name="secondSlot"><span> i am slot two </span></slot><slot></slot> </p>' }
       }
   }).$mount('#app')
</script>

经测试多个相同slot名的元素将依次插入对应的预占位置,无slot名的元素依次插入无name值的slot位置,即表现为<p> i am children<span> i am tangerine </span><span> i am orange </span><span> i am apple </span><span> who am i ? </span><span> who are you ? </span></p>

三、实例

一个具有默认显示的<Alert>组件
当我们想要有 Success、Warning、Error 等不同样式和不同内容的 alert 时,我们会希望这样使用:

    <Alert type="success">
      <strong>success: </strong> this is some info about success;
    </Alert>
    <Alert type="wraning">
      <strong>wraning: </strong> this is some info about wraning;
    </Alert>
    <Alert type="error">
      <strong>error: </strong> this is some info about error;
    </Alert>

要实现这个功能 具体如下:
Alert.js

<template>
  <div :class="typeClass">
    <slot><strong>default: </strong> this is some info about default; </slot>
  </div>
</template>
<script>
  import Vue from 'vue';
  export default Vue.extend({
    props: [ 'type' ],
    computed: {
        typeClass()
        {
          return `${ this.type||'default' }-alert`
        }
    }
  })
</script>
<style scoped>
    .success-alert{}
    .error-alert{}
    .wraning-alert{}
    .default-alert{}
</style>

app.js

<template>
    <div id="app">
         <Alert></Alert>
         <Alert type="success"> status_success </Alert>
    <div>
</template>

<script>
import Vue from 'vue';
import Alert from 'Alert';

new Vue({
  components: { Alert }
}).$mount('#app')
</script>

四、进阶 - 结合render函数

预知后事如何,且听下回分解

相关文章

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

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

  • slot(插槽)

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

  • slot内容分发

    概述 通俗地说,slot内容分发要解决的问题就是:父组件需要在子组件内放一些DOM,那么这些DOM是显示、不显示、...

  • slot内容分发

    1.编写组件my-slot1;2.在组件里添加(默认插槽或者具名插槽); 3.调用该组件(my-slot1),往插...

  • slot内容分发

    slot: 位置;狭槽;水沟;硬币投币口 ---from有道词典个人理解为位置预占, 有新增则覆盖, 无则显示预占...

  • Slot 分发内容

    需要做个页面,涉及 查看、编辑,来列一下流程: 路由传 Id 过来,捕获 Id 使用 Ajax 获取这个 Id 的...

  • slot内容分发

    本意:位置、槽如果想显示wbs17022中的内容 => wbs17022...

  • slot分发内容

    一、slot定义 中文插槽,混合父组件的内容与子组件自已模板的方式其中 Vuejs 把 slot 元素作为原始内容...

  • 27.Vue slot内容分发-解构

    Vue slot的使用参考:26.Vue slot内容分发 什么是slot分发解构:官方定义:如果一个 JavaS...

  • vue 学习记录之slot分发内容

    slot分发内容 不具名slot用来备用插入,子组件只有不具名的slot时候,父组件才回调用slot内容,如果子组...

网友评论

      本文标题:slot内容分发

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