vue路由是能够经过组件的形式把全部的组件组装成为一个应用程序,当咱们须要的时候,将这个组件映射到路由,而后告诉Vue 咱们在哪里渲染它们。html
路由是咱们浏览器的一个地址。vue
当咱们加载一个程序时,因为它加载的内容特别多,因此会比较缓慢,可是咱们加载完以后,咱们到页面里进行切换,它的切换速度会变得特别快。缘由是由于咱们将全部的程序都放在一个页面里面了,咱们将它加载时全部的功能全部的主键都加载到一个页面去了,因此它加载的速度特别慢,可是加载完以后咱们再进行后续的操做时,它的切换速度和反应速度回特别快。vue-router
路由最主要的配置是在于切换和跳转页面这两个方面。浏览器
<!DOCTYPE html> <html> <meta charset="utf-8"> <title>Vue 路由的配置</title> <head> <meta charset="utf-8"> <title>Vue 内置指令</title> <script src="Vue.min.js"></script> <script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script> <script src="https://unpkg.com/vue-router@3.1.2/dist/vue.js"></script> <style> * { padding: 0; margin: 0; } body { background: #F4F5F6; } a { text-decoration: none; color: #000; } #main { width:980px; margin: 0 auto; margin-top: 80px; } #main-left { width: 180px; float: left; } #main-left a { display: block; width: 100%; height: 50px; line-height: 50px; text-align: center; } #main-right { width: 730px; float: right; background: #fff; padding: 20px; } .active { color: #007Aff !important; } </style> </head> <body> <div id="main"> <!-- 导航栏 --> <div id="main-left"> <router-link to="/home" @click.native="changeIndex(1);" :class="{active : index == 1}">网站首页</router-link> <router-link to="/about" @click.native="changeIndex(2);" :class="{active : index == 2}">关于咱们</router-link> </div> <!-- 对应显示的内容 --> <div id="main-right"> <router-view></router-view> </div> </div> </body> <script> // 定义组件模块 const Home = { template: '<div><h1>Vue</h1><p>Vue课程</p></div>' }; const About = { template: '<div><div><router-link to="/about/ha">关于内容</router-link> | <router-link to="/home/hb">关于课程</router-link></div>' }; const Ha = { template: '<div><h1>关于内容</h1><p>关于内容........</p></div>'}; const Hb = { template: '<div><h1>关于课程</h1><p>关于课程........</p></div>'}; // 定义路由 const router = new VueRouter({ routes: [ { path: '/', redirect: '/home' }, { path: '/home', component: Home}, { path: '/about', component: About, children: [ { path: 'ha', component: Ha }, { path: 'hb', component: Hb } ] }, ] }); // 建立vue实例并使用路由配置 var vm = new Vue({ router, data: { index: 1 }, methods: { changeIndex: function(index) { this.index = index; } } }).$mount('#main'); </script> </html>