vue2.0路由

Posted 安筱雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue2.0路由相关的知识,希望对你有一定的参考价值。

现在用vue-cli搭建的环境里面vue-router是下载好的

vue2.0路由方式和以前也有些不同

没了了map和start方法

目录结构如上图

这里有三个文件,app.vue显示,main.js控制路由,goods.vue为跳转页面

app.vue

template>
  <div id="app">
    <v-header></v-header>
    <div class="tab">
      <div class="tab-item">
        <router-link to="/goods">商品</router-link>
      </div>
      <div class="tab-item">评论</div>
      <div class="tab-item">商家</div>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
  import header from \'./components/header/header.vue\';

  export default{
      components:{
          \'v-header\':header
      }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  #app
     .tab
        display:flex
        width:100%
        height:40px
        line-height:40px
        .tab-item
          flex:1
          text-align center

</style>

 main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from \'vue\'
import VueRouter from \'vue-router\'
import App from \'./App\'
import goods from \'./components/goods/goods.vue\';
import ratings from \'./components/ratings/ratings.vue\';
import seller from \'./components/seller/seller.vue\';

import "./common/stylus/index.styl";
Vue.use(VueRouter);
Vue.config.productionTip = false;


const routers=[
  {path:\'/goods\',component:goods},
  {path:\'/ratings\',component:ratings},
  {path:\'/seller\',component:seller}
]
const router=new VueRouter({
  linkActiveClass:"active",
  mode:"history",
  routes:routers
});

const app = new Vue({
  router
}).$mount(\'#app\')

new Vue({
  template: \'<App/>\',
  components: { App },
  router: router
}).$mount(\'#app\');

router.push(\'/goods\')

 

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from \'vue\'
import VueRouter from \'vue-router\'
import App from \'./App\'
import goods from \'./components/goods/goods.vue\';
import ratings from \'./components/ratings/ratings.vue\';
import seller from \'./components/seller/seller.vue\';

import "./common/stylus/index.styl";
Vue.use(VueRouter);
Vue.config.productionTip = false;


const routers=[
  {path:\'/goods\',component:goods},
  {path:\'/ratings\',component:ratings},
  {path:\'/seller\',component:seller}
]
const router=new VueRouter({
  linkActiveClass:"active",
  mode:"history",
  routes:routers
});

const app = new Vue({
  router
}).$mount(\'#app\')

new Vue({
  template: \'<App/>\',
  components: { App },
  router: router
}).$mount(\'#app\');

router.push(\'/goods\')

 

以上是关于vue2.0路由的主要内容,如果未能解决你的问题,请参考以下文章

vue2.0路由进阶

vue2.0-路由嵌套动态路由跳转

vue2.0的变化

vue2.0路由嵌套

Vue2.0的变化———vue2.0动画的变化vue-2.0路由的变化

vue2.0-路由配置