用vue+tp5写一个抢购商城(二)首页路由和基础设置

刚建立的项目,我是从一个大的导航入手,可以明确有五个大的页面,所以这里配置路由的时候设置这几个页面
1,index.js创建路由规则

import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import chanp from '@/components/nav/chanp'
import news from '@/components/nav/news'
import lianx from '@/components/nav/lianx'
import header from '@/components/common/header'//公共头部
import footer from '@/components/common/footer'//公共尾部

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index   //首页
    },
  
    {
      path: '/chanp',
      name: 'chanp',
      component: chanp
    },
    {
      path: '/news',
      name: 'news',
      component: news
    },
    {
      path: '/lianx',
      name: 'lianx',
      component: lianx
    }
  ],
  mode:'history'//去掉链接中的#
  
})

这里的index.js路由设置好了后还要在app.vue里面调用路由和模板

<template>
  <div id="app">
    <app-header></app-header>
    <router-view/>
    <app-footer></app-footer>
  </div>
</template>

<script>
import AppHeader from './components/common/header.vue'
import AppFooter from './components/common/footer.vue'
export default {
  name: 'App',
  components:{//要引入文件先注册组件
    AppHeader,
    AppFooter
  }
}

</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  
}
</style>

然后在components文件下创建相应的页面,方便跳转
首页:index.vue

<template>
  
  <div>首页样式</div>
</template>
<script>
export default{
    name:"index"//必须命名文件才有效,其他的文件也是如此
    
}
</script>

这里在举例头部common/header.vue

<template>
  <div class="header">
      <img src="/static/img/1.png"/>

      <ul>
         <li v-for="(item,index) in items" v-bind:id="item.id" v-on:click="itemClick(item.id)" v-bind:class="{checked:item.id==nowId}">{{item.nav}}</li>
         <!--这里的用循环输出数组中的导航名称item和绑定id的点击事件,实现对应的跳转-->
      </ul>
      <ul>
          <li>首页</li>
          <li @click="goChanp">产品</li>
      </ul>
  </div>
</template>
<script>
export default{
    name:'header',
    data(){
        return{
              items:[
                  {nav:'首页',id:''},
                  {nav:'所有商品',id:'chanp'},
                  {nav:'最新揭晓',id:'news'},
                  {nav:'购物车',id:'goods'},
                  {nav:'我的抢购',id:'lianx'}
              ],
              nowId:'',
             
        }
      
    },
    methods:{
        goChanp(){
            this.$router.push({path:'/chanp'});
        },
        itemClick(id){
           this.nowId=id;
            this.$router.push({
                path:'/'+this.nowId
            })
        }

       

    }
}
</script>
<style>
.header{
    width:100%;
    height:30px;
}
.header ul li{
    float:left;
    width:100px;
    height:30px;
    line-height:30px;
    display:block;
    list-style:none;
    background:green;
    border:1px solid green;
    border-radius:5px;
    margin-left:50px;
    transition:1s;
}
.header ul{width:50%;margin:5px auto;}
.header ul li:hover{background:gray;color:aliceblue;border:1px solid aliceblue;}
</style>

标签: 无

发表评论:

whatsapp营销