VUE学习:vue基础16————插槽

提示:
VUE学习:vue基础16————插槽

VUE学习:vue基础16————插槽


前言

本文继续学习Vue相关内容。


提示:以下是本篇文章正文内容,下面案例可供参考# 表单元素的事件绑定问题

插槽

<body>

<div id="app">
    <test>你好,世界</test>
   
</div>

</body>
<script>
    // 全局组件
    Vue.component('test', Vue.extend({
        template: '<h2>全局组件</h2>'
    }));

    let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components: {
          
        }
    });
</script>

如上代码所示:

**你好,世界**中的“你好,世界”不会显示。

解决此问题我们采用了插槽。

插槽标签slot的简单使用

插槽内可以包含任意内容或html标签

<body>
<div id="app">
    <my-link>百度</my-link>
    <my-link>知乎</my-link>
    <!--            插槽内可以包含任意内容或html标签-->
    <my-link><b><i>小米</i></b></my-link>
    <!--            slot标签内给定的内容为插槽的默认值-->
    <my-link></my-link>

</div>

<template id="t1">
    <div>
        <a href="https://www.baidu.com">
<!--            插槽内可以包含任意内容或html标签-->
<!--            slot标签内给定的内容为插槽的默认值-->
            <slot>未知网站</slot>
            你好,世界
        </a>
    </div>
</template>
</body>
<script>
    let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components:{
            'my-link':{
                template:"#t1",
            }
        }
    });
</script>

演示效果

在这里插入图片描述

插槽的高级使用

当模板中存在多个插槽时,可以给插槽设置name属性,加以区分

插槽name的默认值为default,并且在调用组件是,会自动优先向没有name的插槽中填充内容,如果存在多个没有设置name的插槽,或者name相同的插槽,则按照顺序从上至下填充最上面的插槽

<body>
<div id="app">
    <layout>
        <h3>我来组成头部</h3>
        <h3>我来组成身体</h3>
        <h3>我来组成尾部</h3>
    </layout>

</div>
<template id="t2">
    <div>
        <header>
            <slot></slot>
        </header>
        <div>
            <slot> </slot>
        </div>
        <footer>
            <slot></slot>
        </footer>
    </div>
</template>
</body>
<script>
    let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components:{
            layout:{
                template: "#t2",
            }
        }
    });
</script>

指定插槽位置需要template标签来完成,并设置name

<body>
<div id="app">
    <layout>
        <template v-slot:foot>
            <h3>我来组成尾部</h3>
        </template>
        <template v-slot:head>
            <h3>我来组成头部</h3>
        </template>
        <h3>我来组成身体</h3>
    </layout>

</div>
<template id="t2">
    <div>
        <header>
            <slot name="head"></slot>
        </header>
        <div>
            <slot> </slot>
        </div>
        <footer>
            <slot name="foot"></slot>
        </footer>
    </div>
</template>

</body>
<script>
    let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components:{
            layout:{
                template: "#t2",
            }
        }
    });
</script>

v-slot:的缩写为#

<body>
<div id="app">
    <layout>
        <template v-slot:foot>
            <h3>我来组成尾部</h3>
        </template>
        <!--缩写为#-->
        <template #head>
            <h3>我来组成头部</h3>
        </template>
        <h3>我来组成身体</h3>

        <h3>我来组成头部</h3>
        <h3>我来组成身体</h3>
        <h3>我来组成尾部</h3>
    </layout>

</div>
<template id="t2">
    <div>
        <header>
            <slot name="head"></slot>
        </header>
        <div>
            <slot> </slot>
        </div>
        <footer>
            <slot name="foot"></slot>
        </footer>

    </div>
</template>

</body>
<script>
    let vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components:{
            layout:{
                template: "#t2",
            }
        }
    });
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
组件是 Vue.js 中最重要的概念之一。组件可以让我们将 UI 拆分为独立、可复用的部件,使得代码更加清晰、易于维护。在 Vue.js 中,组件可以分为全局组件和局部组件,其中全局组件可在任何地方使用,而局部组件只能在其父组件中使用。 定义组件时,需要使用 Vue.component() 方法,该方法需要传入两个参数:组件名称和组件配置对象。组件名称应该采用 kebab-case(短横线分隔命名)格式,以便在 HTML 中使用。 示例代码如下: ```javascript // 定义一个名为 button-counter 的新组件 Vue.component('button-counter', { data: function () { return { count: 0 } }, template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>' }) ``` 在上述代码中,我们定义了一个名为 button-counter 的组件,该组件包含一个计数器,每次点击按钮计数器加一。 在 HTML 中使用组件时,需要使用组件名称作为自定义标签来调用组件。示例代码如下: ```html <div id="app"> <button-counter></button-counter> </div> ``` 在上述代码中,我们调用了 button-counter 组件,并将其渲染到了 id 为 app 的 div 元素中。 除了组件的 data 和 template 属性外,还可以使用 props 属性来传递组件之间的数据。使用 props 时,需要在组件的配置对象中定义 props 属性,并在 HTML 中使用 v-bind 指令来传递数据。 示例代码如下: ```javascript // 定义一个名为 todo-item 的新组件 Vue.component('todo-item', { props: ['todo'], template: '<li>{{ todo.text }}</li>' }) // 创建一个 Vue 实例 var app = new Vue({ el: '#app', data: { groceryList: [ { id: 0, text: '蔬菜' }, { id: 1, text: '水果' }, { id: 2, text: '奶酪' } ] } }) ``` 在上述代码中,我们定义了一个名为 todo-item 的组件,并使用 props 属性定义了一个名为 todo 的 prop。在 HTML 中,我们使用 v-bind 指令将 groceryList 数组中的每个对象传递给了 todo-item 组件。示例代码如下: ```html <div id="app"> <ul> <todo-item v-for="item in groceryList" v-bind:todo="item" v-bind:key="item.id"></todo-item> </ul> </div> ``` 在上述代码中,我们使用 v-for 指令遍历 groceryList 数组,并使用 v-bind 指令将数组中的每个对象传递给了 todo-item 组件。注意,我们还需要使用 v-bind:key 指令来为每个列表项指定一个唯一的 key 值。 插槽Vue.js 中另一个重要的概念。插槽可以让父组件在子组件中插入任意的 HTML 内容,使得组件更加灵活、可复用。 在子组件中,使用 <slot> 标签来定义插槽。在父组件中,使用子组件的自定义标签来调用组件,并在标签内部插入 HTML 内容。示例代码如下: ```javascript // 定义一个名为 alert-box 的新组件 Vue.component('alert-box', { template: ` <div class="alert-box"> <strong>Error!</strong> <slot></slot> </div> ` }) // 创建一个 Vue 实例 var app = new Vue({ el: '#app' }) ``` 在上述代码中,我们定义了一个名为 alert-box 的组件,并在组件中定义了一个插槽。在 HTML 中,我们调用了 alert-box 组件,并在标签内部插入了一些 HTML 内容。示例代码如下: ```html <div id="app"> <alert-box> <p>Something bad happened.</p> </alert-box> </div> ``` 在上述代码中,我们调用了 alert-box 组件,并在标签内部插入了一些 HTML 内容。该 HTML 内容会被插入到 alert-box 组件的插槽中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值