vue不具名插槽与具名插槽

vue不具名插槽与具名插槽

需求:面向未来编程,实现在未来中若出现修改内容可以简便的修改,而不需要将全部整改

思路:在组件Tabbar.vue(底部导航)当中使用不具名插槽,Tab-bar-item.vue中写具名插槽以便在App.vue中方便获取和更改

Tabbar组件:
在这里插入图片描述
TabBarItem组件(Tab-bar-item.vue):
在这里插入图片描述

目录:在这里插入图片描述

Tabbar.vue

<template>
    <div id="tabbar">
        <slot></slot>
   </div>
</template>

<script>
    export default {
        name: "Tabbar"
    }
</script>

<style scoped>
    #tabbar{
        width: 100%;
        position: fixed;
        bottom: 0;
        display: flex;
        justify-content: space-around;
        background-color: #f6f6f6;
        box-shadow: 0 -1px 1px rgba(100,100,100,.1);
    }

</style>

Tab-bar-item.vue

<template>
    <div class="tab-bar-item">
        <slot name="slot-img"></slot>
        <slot name="slot-text"></slot>
    </div>
</template>

<script>
    export default {
        name: "tab-bar-item",
        props:{
            path:String
        }
    }
</script>

<style scoped>
    @import "../../assets/css/base.css";
    .tab-bar-item{
        text-align: center;
        height: 49px;   /*移动端底部导航的固定像素,一般都是49px*/
    }
</style>

在App.vue中想使用什么就使用什么
App.vue:

<template>
 <div>
    <Tabbar>
        <TabBarItem path="/home">
            <template v-slot:slot-img>
                <img src="./assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>首页</div>
            </template>
        </TabBarItem>
        <TabBarItem path="/category">
            <template v-slot:slot-img>
                <img src="./assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>分类</div>
            </template>
        </TabBarItem>
        <TabBarItem path="/cart">
            <template v-slot:slot-img>
                <img src="./assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>购物车</div>
            </template>
        </TabBarItem>
        <TabBarItem path="/profile">
            <template v-slot:slot-img>
                <img src="./assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>我的</div>
            </template>
        </TabBarItem>
    </Tabbar>
     <router-view></router-view>
 </div>
</template>

<script>
    import Tabbar from "./components/Tabbar/Tabbar";
    import TabBarItem from "./components/Tabbar/Tab-bar-item"
    export default {
        name:'App',
        components:{
            Tabbar,
            TabBarItem
        }
    }
</script>
<style>
    .tab-bar-item img{
        width: 24px;
        height: 24px;
    }
</style>

注:下一篇为vue修改插槽的样式

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值