vue-router路由


vue-router路由(页面跳转)

介绍

Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:

  • 嵌套的路由/视图表
  • 模块化的、基于组件的路由配置
  • 路由参数、查询、通配符
  • 基于 Vue.js 过渡系统的视图过渡效果
  • 细粒度的导航控制
  • 带有自动激活的 CSS class 的链接
  • HTML5 历史模式或 hash 模式,在 IE9 中自动降级
  • 自定义的滚动条行为

安装 vue-router

安装方式:

  1. cmd 进入项目目录
  2. 利用idea终端

然后,输入以下命令

npm install vue-router --save-dev

如果在一个模块化工程中使用它,必须通过 Vue.use()明确安装路由功能

import Vue from 'vue'
import vueRouter from 'vue-router'  //1

//安装路由
Vue.use(vueRouter); //2

路由模板搭建

1.先删除没有用的东西

2.components 目录下存放我们自己编写的组件

3.定义一个 Content.vue 的组件

<template>
  <h1>内容页</h1>
</template>

<script>
    export default {
        name: "content"
    }
</script>

<style scoped>

</style>

4.安装路由,在 src 目录下新建一个文件夹:router,专门存放路由

router 下新建 index.js

注意: 设置跳转的组件时,使用 component 单数

// 1 2 3处的名称必须相同

import Vue from 'vue'
import vueRouter from 'vue-router'  //1
import Content from '../components/content'
import Main from '../components/main'

//安装路由
Vue.use(vueRouter); //2

//配置导出路由
export default new vueRouter({  //3
  routes: [
    {
      //路由路径
      path: '/content',
      name: 'content',
      //跳转的组件
      component: Content
    },
    {
      //路由路径
      path: '/main',
      name: 'main',
      //跳转的组件
      component: Main
    }
  ]
});

5.在 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 App from './App'
import vueRouter from 'vue-router'
import router from './router'   //自动扫描里面的路由配置

//关闭生产模式下的提示
Vue.config.productionTip = false;

//显式使用vueRouter
Vue.use(vueRouter);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  //配置路由
  router,
  components: { App },
  template: '<App/>'
});

6.在 App.vue 中使用路由

<template>
  <div id="app">
    <router-link to="/main">首页</router-link>
    <router-link to="/content">内容页</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
  import content from "./components/content";

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

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

测试

1.模板搭建好后,我们可以在 component 目录下新建一个组件,如:

allen.vue

<template>
  <h1>allen</h1>
</template>

<script>
    export default {
        name: "allen"
    }
</script>

<style scoped>

</style>

2.然后,在 index.js 中配置路由

// 1 2 3处的名称必须相同

import Vue from 'vue'
import vueRouter from 'vue-router'  //1
import Content from '../components/content'
import Main from '../components/main'
import allen from '../components/allen'

//安装路由
Vue.use(vueRouter); //2

//配置导出路由
export default new vueRouter({  //3
  routes: [
    {
      //路由路径
      path: '/content',
      name: 'content',
      //跳转的组件
      component: Content
    },
    {
      //路由路径
      path: '/main',
      name: 'main',
      //跳转的组件
      component: Main
    },
    {
      //路由路径
      path: '/allen',
      name: 'allen',
      //跳转的组件
      component: allen
    }
  ]
});

3.最后,在 App.vue 中使用路由即可。

<template>
  <div id="app">
    <router-link to="/main">首页</router-link>
    <router-link to="/content">内容页</router-link>
    <router-link to="/allen">allen</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
  import content from "./components/content";

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

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

文章作者: Hailong Gao
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hailong Gao !
评论
  目录