1. 程式人生 > >vue3.0 仿微信實時通訊Tab切換與子路由

vue3.0 仿微信實時通訊Tab切換與子路由

需求:底部有tab切換;點選當前會顯示對應的子頁面

效果圖

tab子元件:

<template>
  <div class="tabbar">
    <router-link 
      class="tab-item"
      v-for="(item,index) in data"
      :key="index"
      :to="item.path"
      active-class="is-selected"
      >
      <div class="tab-item-icon">
        <i :class="'fa fa-'+item.icon"></i>
      </div>

      <div class="tab-item-label">
        {{item.title}}
      </div>
    </router-link>
  </div>
</template>
<script>
export default {
  name: 'tabbar',
  props: {
    data: Array
  }
};
</script>
<style scoped>
.tabbar {
  height: 55px;
  box-sizing: border-box;
  width: 100%;
  position: fixed;
  bottom: 0;
  background-image: linear-gradient(
    180deg,
    #d9d9d9,
    #d9d9d9 50%,
    transparent 0
  );
  background-size: 100% 1px;
  background-repeat: no-repeat;
  background-position: 0 0;
  background-color: #fafafa;
  display: flex;
  text-align: center;
}
.tab-item {
  display: block;
  padding: 7px 0;
  flex: 1;
}
.tab-item-icon {
  width: 24px;
  height: 24px;
  margin: 0 auto 5px;
}
.tab-item-icon i {
  font-size: 1.3rem;
}
.tab-item-label {
  color: inherit;
  font-size: 12px;
  line-height: 1;
}
a {
  text-decoration: none;
  color: #888;
}

.is-selected {
  color: #20af0e;
}
</style>

 

建立四個頁面:

Chats.vue,Contacts.vue,Discover.vue,Me.vue 

 路由頁面:

import Index from './views/Index.vue';

Vue.use(Router);

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'index',
      redirect: '/chats',//這裡給一個重定向(預設)的路徑是因為頁面一進入的時候有一個圖示需要有選中狀態
      component: Index,
      children: [
        {
          path: '/chats',
          name: 'chats',
          component: () => import('./views/Chats.vue')
        },
        {
          path: '/contacts',
          name: 'contacts',
          component: () => import('./views/Contacts.vue')
        },
        {
          path: '/discover',
          name: 'discover',
          component: () => import('./views/Discover.vue')
        },
        {
          path: '/me',
          name: 'me',
          component: () => import('./views/Me.vue')
        }
      ]
    },)

index.vue頁面配置

<template>
  <div class="index">
    <!--元件支援使用者在具有路由功能的應用中點選導航-->
    <router-view></router-view>
    <TabBar :data="tabbarData"/>
  </div>
</template>
<script>
import TabBar from '../components/TabBar';
export default {
  name: 'index',
  data() {
    return {
      tabbarData: [
//這裡的icon是引入的字型圖示庫的圖示,你們可以寫img路徑也可以,根據實際情況修改
        { title: '微信', icon: 'comment', path: '/chats' },
        { title: '通訊錄', icon: 'address-book', path: '/contacts' },
        { title: '發現', icon: 'compass', path: '/discover' },
        { title: '我', icon: 'user', path: '/me' }
      ]
    };
  },
  components: { TabBar }
};
</script>
<style scoped>
.index {
  width: 100%;
  height: 100%;
}
</style>

子頁面寫法:(這裡只寫一頁面;其它三個頁面寫法類似就不粘上來了)

<template>
  <div>
    <Header title="微信" btn_icon="plus"/>
  </div>
</template>
<script>
import Header from '../components/Header';
export default {
  name: 'tabbar',
  components: {
    Header
  }
};
</script>