zoukankan      html  css  js  c++  java
  • vueslot插槽

    slot插槽

    vue里提供了一种将父组件的内容和子组件的模板整合的方法:内容分发,通过slot插槽来实现。
    slot

    匿名插槽

    在父组件中使用子组件的时候,在子组件标签内部写入内容,在子组件的模板中可以通过 来使用

    <body>
        <div id="app">
            <!-- 匿名插槽 -->
            <tem>
                <div>内容a</div>
                <div>内容B</div>
            </tem>
        </div>
        <template id="tem">
            <div>
                <slot></slot>
                <h2>标题</h2>
                <slot></slot>
            </div>
        </template>
    </body>
    <script>
        Vue.component("tem", {
            template: "#tem",
        })
        new Vue({
            el: "#app",
    
        })
    </script>
    结果:内容a
         内容B
         标题
         内容a
         内容B

    缺点:如果多个 不能自己设定每个放置的位置

    具名插槽

    父组件在子组件标签内写的多个内容我们可以给其设置slot属性来命名,在子组件的模板通过通过使用带有name属性的slot标签来放置对应的slot。

    <body>
        <div id="app">
            <!-- 具名插槽 -->
            <tem>
                <div slot="a"> a内容</div>
                <div slot="b">内容b</div>
            </tem>
        </div>
        <template id="tem">
            <div>
                <slot name="a"></slot>
                <h2>标题</h2>
                <slot name="b"></slot>
            </div>
        </template>
    </body>
    <script>
        Vue.component("tem", {
            template: "#tem",
        })
        new Vue({
            el: "#app",
    
        })
    </script>
    结果:a内容
         标题
         内容b
    v-slot作用域插槽

    新版本2.6支持v-slot方式

    注意:- 要引入2.6版本以上的vue.js)

    ​ - 在使用时,必须在template标签内

    <body>
        <div id="app">
            <hello>
                  <!-- 在template中设置v-slot a='info'接收数据数组对象,通过info. 调用数据 -->
                <template v-slot:a='info'>{{info.msg1}}</template>
                  <!-- v-slot: 可简写成 # ,可以通过ES6的解构获取对象-->
                <template #a='{arr}'>{{arr}}</template>
            </hello>
        </div>
        <template id="hello">
            <div>
                 <!-- 在组件模板的slot中绑定数据 -->
                <slot name="a" :msg1='msg'></slot>
                <h3>标题</h3>
                <slot name="b" :arr="arr"></slot>
            </div>
        </template>
    </body>
    <script>
        Vue.component('hello', {
            template: "#hello",
            data() {
                return{
                msg: "hello world",
                arr:[1,2,3,4,5]
            }
            }
        })
        new Vue({
            el: "#app",
        })
    </script>
    请用今天的努力,让明天没有遗憾。
  • 相关阅读:
    Elasticsearch URI search 查询语法整理
    在linux下执行git clone、git pull 、git push等操作免密
    Gitlab 安装、升级、备份、恢复、汉化等
    python 使用xlsxwriter 写入数据时,当数据中链接的后面包含空格时(如:"http://*** "),导出问题打开报错
    offsetLeft && left
    DOM事件绑定方式
    获取不到offsetHeight问题
    jquery 图片预加载
    模仿JQuery 的添加多个事件的原理
    当节点结构变化的时候要重新获取 不然之前的变量还是以前的节点结构
  • 原文地址:https://www.cnblogs.com/cupid10/p/15617706.html
Copyright © 2011-2022 走看看