vue 导航跳转案例

vue 导航跳转案例

创建vue项目的配置:

在这里插入图片描述
选择3.0x版本

目录结构:

在这里插入图片描述
index.js:

import { createRouter, createWebHistory } from 'vue-router'

const Home=()=>import('../views/home')
const Cart=()=>import('../views/cart')
const Category=()=>import('../views/category')
const Profile=()=>import('../views/profile')

const routes = [
  {
    path:'',
    redirect:Home
  },
  {
    path:'/home',
    component:Home
  },
    {
    path:'/cart',
    component:Cart
  },
    {
    path:'/category',
    component:Category
  },
    {
    path:'/profile',
    component:Profile
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

App.vue:

<template>
 <div>
    <Tabbar></Tabbar>
     <router-view></router-view>
 </div>
</template>

<script>
    import Tabbar from "./components/Tabbar/Tabbar";
    export default {
        name:'App',
        components:{
            Tabbar
        }
    }
</script>
<style>
</style>

Tabbar.vue:

<template>
    <div id="tabbar">
        <TabBarItem path="/home" active-color="red">
            <template v-slot:slot-img>
                <img src="../../assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-active-img>
                <img src="../../assets/img/1.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>首页</div>
            </template>
        </TabBarItem>
        <TabBarItem path="/category" active-color="red">
            <template v-slot:slot-img>
                <img src="../../assets/img/2.png" alt="">
            </template>
            <template v-slot:slot-active-img>
                <img src="../../assets/img/1.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-active-img>
                <img src="../../assets/img/1.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-active-img>
                <img src="../../assets/img/1.png" alt="">
            </template>
            <template v-slot:slot-text>
                 <div>我的</div>
            </template>
        </TabBarItem>
   </div>
</template>

<script>
    import TabBarItem from "./Tab-bar-item"
    export default {
        name: "Tabbar",
        components:{
            TabBarItem
        }
    }
</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);
    }
    .tab-bar-item img{
        width: 24px;
        height: 24px;
    }
</style>

Tab-bar-item.vue:

<template>
    <div class="tab-bar-item" @click="itemClick">
        <slot v-if="!isActived" name="slot-img"></slot>
        <slot v-else name="slot-active-img"></slot>

        <!--方法一:子组件中直接设置颜色-->
<!--        <div :class="{active:isActived}">-->
<!--            <slot name="slot-text"></slot>-->
<!--        </div>-->
        <!--方法二:在父组件App中修改设置颜色        -->
        <div :style="activeStyle">
            <slot name="slot-text"></slot>
        </div>

    </div>
</template>

<script>
    export default {
        name: "tab-bar-item",
        computed:{
            isActived(){
                return this.$route.path==this.path
            },
            activeStyle(){
                return this.isActived?{color:this.activeColor}:{}
            }
        },
        props:{
            path:String,
            activeColor:{
                type:String,
                default:'deepSkyblue'
            }
        },
        methods:{
            itemClick(){
                console.log(this.$route)
                console.log(this.path)
                this.$router.push(this.path)
            }
        }
    }
</script>

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

</style>

views底下的文件夹所有的index.vue都一样(测试)
views–>index.vue:

<template>
    <div>
        <h2>我是Cart</h2>
    </div>
</template>

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

<style scoped>

</style>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值