vue导航栏制作

1,在components新建commnn目录,然后再新建nav目录,在此目录下新建nav-bottom.vue文件和nav-item.vue文件

2,nav-bottom.vue中的内容:

    

 1 <template>
 2   <div>
 3     <div class="nav">
 4       <slot></slot>   //插槽
 5     </div>
 6   </div>
 7 </template>
 8 
 9 <script>
10   export default {
11     name: "nav-bottom"
12   }
13 </script>
14 
15 <style scoped>
16   .nav {
17     display: flex;
18     position: fixed;
19     bottom: 0;
20     left: 0;
21     right: 0;
22     background-color: #7b7b7b;
23     height: 49px;
24     text-align: center;
25   }
26 </style>

3,Nav-item代码:

  

<template>
  <div class="nav-item" :class="{isactive:isactive}">
    <div @click="change">
      <slot name="font-image"></slot>  <--!显示字体图片-->
      <slot name="font"></slot>    <--!显示文字-->
    </div>
  </div>
</template>

<script>
  export default {
    name: "nav-item",
    props: {
      path :String  // 传递路径
    },
    computed: {
      isactive () {
        return this.$route.path.indexOf(this.path) !== -1
      }
    },
    methods: {
      change () {
        if (this.isactive===false) {
          this.$router.push(this.path)
        }

      }
    }
  }
</script>

<style scoped>
  .nav-item {
    flex: 1;
  }
  .isactive {
    color: red;
  }
</style>

4,设置导航下的每个字路由页面:

  shopping  此目录下创建对应的vue文件

  profile   此目录下创建对应的vue文件

  homepage  此目录下创建对应的vue文件
  classify   此目录下创建对应的vue文件

5,设置路由:

import Router from 'vue-router'
import Vue from 'vue'

const home = () => import('../components/homepage/Home_msg')
const classify = () => import('../components/classify/Classify')
const shopping = () => import('../components/shopping/Shopping')
const profile = () => import('../components/profile/Profile')


Vue.use(Router)
const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path:'/profile',
    component: profile
  },
  {
    path:'/classify',
    component: classify
  },
  {
    path:'/shopping',
    component: shopping
  },
  {
    path:'/home',
    component: home
  }
]
const router = new Router({
  routes,
  mode: 'history'
});
export default router

6,抽离组件新建navgation.vue:

 1 <template>
 2   <div class="aaa">
 3      <nav-item path="home" colorstyle="blue">
 4         <span class="iconfont" slot="font-image"></span>
 5         <div slot="font">首页</div>
 6      </nav-item>
 7 
 8       <nav-item path="classify" colorstyle="green">
 9          <span class="iconfont" slot="font-image"></span>
10         <div slot="font">分类</div>
11       </nav-item>
12 
13       <nav-item path="shopping" colorstyle="hotpink">
14         <span class="iconfont" slot="font-image"></span>
15         <div slot="font">购物车</div>
16       </nav-item>
17 
18       <nav-item path="profile">
19         <span class="iconfont" slot="font-image"></span>
20          <div slot="font">我的</div>
21       </nav-item>
22   </div>
23 </template>
24 
25 <script>
26   import navItem from "./nav-item"
27   export default {
28     name: "navgative",
29     components: {
30       navItem
31     }
32   }
33 </script>
34 
35 <style scoped>
36      @import "../assets/images/style.css";
37 
38      @font-face {font-family: 'iconfont';
39     src: url('../assets/images/fonts/icomoon.eot');
40     src: url('../assets/images/fonts/icomoon.eot?#iefix') format('embedded-opentype'),
41     url('../assets/images/fonts/icomoon.woff') format('woff'),
42     url('../assets/images/fonts/icomoon.ttf') format('truetype'),
43     url('../assets/images/fonts/icomoon.svg#iconfont') format('svg');
44 }
45 .iconfont{
46     font-family:"iconfont" !important;
47     font-size:16px;font-style:normal;
48     -webkit-font-smoothing: antialiased;
49     -webkit-text-stroke-width: 0.2px;
50     -moz-osx-font-smoothing: grayscale;
51     /*vertical-align: middle;*/
52 }
53   .aaa {
54     display: flex;
55     position: fixed;
56     bottom: 0;
57     left: 0;
58     right: 0;
59   }
60 </style>
navgation.vue

7,app.vue引入组件:

  

<template>
  <div id="app">
    <router-view></router-view>
    <navBottom>
        <navgative></navgative>
    </navBottom>
  </div>
</template>

<script>
  import navBottom from './components/nav-bottom'
  import navgative from './components/navgative'
export default {
  name: 'app',
  components: {
    navBottom,
    navgative
  }
}
</script>

<style>

</style>
app.vue

8,npm run serve 运行

猜你喜欢

转载自www.cnblogs.com/yitd/p/11818377.html
今日推荐