美文网首页
vue 插槽slot

vue 插槽slot

作者: Gino_Li | 来源:发表于2019-04-11 20:44 被阅读0次

具名插槽

使用的时候将需要的内容写在template标签中,并且通过v-slot:name(name表示插槽的名字)指令来指定插入的位置.

<body>
    <div id="app">
        <tab>
            <template v-slot:header>
                <div>我是插到header里面的元素</div>
            </template>
            <template v-slot:section>
                <div>我插入到了section元素里面</div>
            </template>
        </tab>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var tab = {
            template:`
                    <div>
                        <header>
                            <input type="text" placeholder="新不了情" />
                            <slot name="header"></slot>
                        </header>
                        <section>
                            <slot name="section"></slot>
                        </section>
                    </div>`
        }
        var vm = new Vue({
            el:"#app",
            components:{
                tab
            }
        })
    </script>
</body>

无名插槽

直接写需要插入的内容即可,内容将会插入到所有无名插槽中

<body>
    <div id="app">
        <tab>
                      <div>hello world</div>
        </tab>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var tab = {
            template:`
                    <div>
                        <footer>
                            <slot></slot>
                        </footer>
                    </div>`
        }
        var vm = new Vue({
            el:"#app",
            components:{
                tab
            }
        })
    </script>
</body>

vue-cli中应用

父组件

<test-one v-bind:list="message" v-on:getSonsData="sonData">
      //无名插槽
    {{getData}}
    插槽

    <template v-slot:head>
        <p>我的内容插到到具名插槽中</p>
    </template>
</test-one>

子组件

        test页面的子组件
        <div class="header">
            <!--具名插槽-->
            <slot name="head"></slot>
        </div>
        {{list}}
        <button @click="toparent">发送数据给父组件</button>
        <!--插槽-->
        <slot></slot>

相关文章

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

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

  • vue插槽

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

  • vue中的slot(插槽)

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

  • vue 插槽的使用

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

  • 18、Vue3 作用域插槽

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

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

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

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

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

  • slot(插槽)

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

  • slot 用法以及使用场景

    Vue的插槽slot,分为3种 匿名插槽 具名插槽 作用域插槽 前两种很好理解,无非就是子组件里定义一个slot占...

  • Vue中Slot的渲染过程

    Vue在通过compiler解析模版中的slot, slot是组件中的插槽,通过解析slot,把slot的name...

网友评论

      本文标题:vue 插槽slot

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