vue2.x 路由TabBar练习

实现效果如图,点击按钮显示页面,活动界面按钮高亮

实现思路

创建项目(使用vue-cli2方式)

删除无用文件

创建base.css,设置基本样式

修改App.vue内容,初步实现样式

<template>
  <div id="app">
    <div id="tab-bar">
      <div class="tab-bar-item">首页</div>
      <div class="tab-bar-item">分类</div>
      <div class="tab-bar-item">购物车</div>
      <div class="tab-bar-item">我的</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
@import url("./assets/css/base");
#tab-bar {
  display: flex;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f2f2f2;
  box-shadow: 0 -1px 1px rgba(0, 0, 0, 0.1);
}

.tab-bar-item {
  flex: 1;
  height: 49px;
  text-align: center;
}
</style>

已初具雏形

底部封装为TabBar组件,内部封装TabBarItem,可传入图片和文字

<!-- components文件夹创建tabbar文件夹,创建TabBar.vue文件 -->
<template>
  <div id="tab-bar">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'TabBar',
  data () {
    return {
    }
  }
}
</script>

<style  scoped>
#tab-bar {
  display: flex;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f2f2f2;
  box-shadow: 0 -1px 1px rgba(0, 0, 0, 0.1);
}
</style>
<!-- components/tabbar文件夹下创建TabBarItem.vue文件-->
<template>
  <div class="tab-bar-item">
    <div><slot name="item-icon"></slot></div>
    <div><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  name: 'TabBarItem',
  data () {
    return {
    }
  }
}
</script>

<style  scoped>
.tab-bar-item {
  flex: 1;
  height: 49px;
  text-align: center;
  font-size: 14px;
}

.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
  margin-bottom: 2px;
}
</style>
<!-- App.vue文件修改 -->
<template>
  <div id="app">
    <tab-bar>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/home.svg" alt="" />
        <div slot="item-text">首页</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/category.svg" alt="" />
        <div slot="item-text">分类</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/shopcart.svg" alt="" />
        <div slot="item-text">购物车</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/profile.svg" alt="" />
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

<script>
import TabBar from './components/tabbar/TabBar.vue'
import TabBarItem from './components/tabbar/TabBarItem.vue'
export default {
  components: { TabBar, TabBarItem },
  name: 'App'
}
</script>

<style>
@import url("./assets/css/base");
</style>

实现效果

 TabBarItem组件中定义高亮图片,定义变量isActive,通过v-if决定显示对应的icon

TabBarItem绑定路由数据

动态决定高亮图片

实现效果

动态设置文本样式

最终效果

简化App.vue文件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个Vue.js 2.x中使用路由导航守卫的案例: ```javascript // main.js import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' Vue.use(VueRouter) const router = new VueRouter({ routes: [ { path: '/', component: Home }, { path: '/dashboard', component: Dashboard, meta: { requiresAuth: true } // 添加 meta 字段,表示需要进行身份验证 }, // 其他路由配置... ] }) router.beforeEach((to, from, next) => { if (to.meta.requiresAuth) { // 判断路由是否需要进行身份验证 // 进行身份验证逻辑,比如检查用户是否登录 if (userLoggedIn) { next() // 用户已登录,继续导航到目标路由 } else { next('/') // 用户未登录,重定向到首页或登录页面 } } else { next() // 路由无需进行身份验证,直接导航到目标路由 } }) new Vue({ router, render: h => h(App) }).$mount('#app') ``` 在上述代码中,我们定义了一个VueRouter实例,并配置了多个路由。其中,`/dashboard` 路由配置中添加了 `meta` 字段,表示需要进行身份验证。 然后,我们使用 `router.beforeEach()` 注册了一个全局前置守卫。在守卫函数中,通过判断目标路由的 `meta` 字段来确定是否需要进行身份验证。如果需要验证身份,则可以在守卫函数中执行相应的逻辑,比如检查用户是否已登录。如果用户已登录,则调用 `next()` 继续导航到目标路由;如果用户未登录,则调用 `next('/')` 进行重定向,可以重定向到首页或登录页面。 最后,我们创建了一个Vue实例,并将路由实例传入,通过 `router-link` 和 `router-view` 实现路由的导航和渲染。 这个案例展示了如何使用路由导航守卫来实现身份验证的功能。在进入需要进行身份验证的页面时,会触发守卫函数进行判断,并根据判断结果进行相应的导航操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值