使用vue-Router2 实现路由功能
来源:互联网 发布:杀牛电击锤价格淘宝 编辑:程序博客网 时间:2024/06/06 19:18
注意:vue-router 2只适用于Vue2.x版本,下面我们是基于vue2.0讲的如何使用vue-router 2实现路由功能。
推荐使用npm安装。
npm install vue-router
一、使用路由
在main.js中,需要明确安装路由功能:
import Vue from 'vue'import VueRouter from 'vue-router'import App from './App.vue'Vue.use(VueRouter)
1.定义组件,这里使用从其他文件import进来
import index from './components/index.vue'import hello from './components/hello.vue'
2.定义路由
const routes = [ { path: '/index', component: index }, { path: '/hello', component: hello },]
3.创建 router 实例,然后传 routes 配置
const router = new VueRouter({ routes})
4.创建和挂载根实例。通过 router 配置参数注入路由,从而让整个应用都有路由功能
const app = new Vue({ router, render: h => h(App)}).$mount('#app')
经过上面的配置之后呢,路由匹配到的组件将会渲染到App.vue里的<router-view></router-view>
那么这个App.vue里应该这样写:
<template> <div id="app"> <router-view></router-view> </div></template>
index.html里呢要这样写:
<body> <div id="app"></div></body>
这样就会把渲染出来的页面挂载到这个id为app的div里了。
二、重定向 redirect
const routes = [ { path: '/', redirect: '/index'}, // 这样进/ 就会跳转到/index { path: '/index', component: index }]
三、嵌套路由
const routes = [ { path: '/index', component: index, children: [ { path: 'info', component: info} ] }]
通过/index/info就可以访问到info组件了
四、懒加载
const routes = [ { path: '/index', component: resolve => require(['./index.vue'], resolve) }, { path: '/hello', component: resolve => require(['./hello.vue'], resolve) },]
通过懒加载就不会一次性把所有组件都加载进来,而是当你访问到那个组件的时候才会加载那一个。对于组件比较多的应用会提高首次加载速度。
五、<router-link>
在vue-router 1中,使用的是
在vue-router 2中,使用了<router-link></router-link>替换1版本中的a标签
<!-- 字符串 --><router-link to="home">Home</router-link><!-- 渲染结果 --><a href="home">Home</a><!-- 使用 v-bind 的 JS 表达式 --><router-link v-bind:to="'home'">Home</router-link><!-- 不写 v-bind 也可以,就像绑定别的属性一样 --><router-link :to="'home'">Home</router-link><!-- 同上 --><router-link :to="{ path: 'home' }">Home</router-link><!-- 命名的路由 --><router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link><!-- 带查询参数,下面的结果为 /register?plan=private --><router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
六、路由信息对象
1.$$route.path
字符串,对应当前路由的路径,总是解析为绝对路径,如 “/foo/bar”。
2.$route.params
一个 key/value 对象,包含了 动态片段 和 全匹配片段,如果没有路由参数,就是一个空对象。
3.$route.query
一个 key/value 对象,表示 URL 查询参数。例如,对于路径 /foo?user=1,则有 $route.query.user == 1,如果没有查询参数,则是个空对象。
4.$route.hash
当前路由的 hash 值 (不带 #) ,如果没有 hash 值,则为空字符串。
5.$route.fullPath
完成解析后的 URL,包含查询参数和 hash 的完整路径。
6.$route.matched
一个数组,包含当前路由的所有嵌套路径片段的 路由记录 。路由记录就是 routes 配置数组中的对象副本(还有在 children 数组)。
综合上述,一个包含重定向、嵌套路由、懒加载的main.js如下:
import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)const router = new VueRouter({ routes:[ { path: '/', redirect: '/index' }, { path: '/index', component: resolve => require(['./components/index.vue'], resolve), children:[ { path: 'info', component: resolve => require(['./components/info.vue'], resolve) } ] }, { path: '/hello', component: resolve => require(['./components/hello.vue'], resolve) }, ]})const app = new Vue({ router, render: h => h(App)}).$mount('#app')
更详细的vue-router功能请参考文档:https://router.vuejs.org/zh-cn/
- 使用vue-Router2 实现路由功能
- 【Vue】Vue-Router2 实现路由功能
- Vue-Router2.X多种路由实现方式
- vue-router2.0,子路由的使用
- 使用Vue-Router 2实现路由功能
- 使用Vue-Router 2实现路由功能
- 使用Vue-Router 2实现路由功能
- 使用Vue-Router 2实现路由功能
- 使用Vue-Router 2实现路由功能
- 使用Vue-Router 2实现路由功能
- Vue-Router2.0使用1
- vue-router2
- vue-router2
- vue+vue-router+vuex,利用vue-router2.2.0新增特性addRoutes实现路由动态加载,菜单动态加载,运用于后台管理系统,路由数据取自数据库
- Vue-router2.0版本的使用
- vue 路由功能
- Webpack2 + Vue2 + Vue-Router2 如何实现懒加载?
- Vue-Router2.0教程
- CentOS_7 Systemd 入门教程:实战篇
- gulpfile.js 备份
- 算法与数据结构实验题 2.3 泡泡的饭碗
- Macaca自动化测试框架
- 【头条】从超融合市场的探索与创新 看浪潮全面云化时代关键词
- 使用vue-Router2 实现路由功能
- java(十七):concurrent(2)—lock和condition
- 手把手教你在win2003_IIS6.0环境下配置主机头发布多个站点
- react-navigation使用技巧
- ffmpeg中遇到UINT64_C’ was not declared in this scope
- pycham看程序执行的时间
- 如何更改linux文件的拥有者及用户组(chown和chgrp)
- 初学Qt:吐槽下keyPressEvent事件
- SpringMVC如何用实体对象接收参数