vue的一个插件库,专门用来实现SPA应用。
ajax
请求获取。(key——value)
。key
为路径, value
可能是function
或者component
。value
是function
,用于处理客户端提交的请求。
服务器接收到一个请求时,根据请求路径找到匹配的函数类来处理请求,返回响应数据。
(资料图片仅供参考)
value
是component
,用于展示页面内容。
当浏览器的路径发生改变时,对应的组件就会显示。
安装vue-router: npm i vue-router@3
ps: vue-router3只能在vue2中使用,vue-router4在vue3中使用。
在main.js
里应用插件:
//引入路由import VueRouter from "vue-router"//使用路由Vue.use(VueRouter)
创建src/router/index.js
//该文件用于创建整个应用的路由器import VueRouter from "vue-router";//引入组件import Home from "../components/Home"import About from "../components/About"//创建并暴露一个路由器export default new VueRouter({ routes: [ { path: "/Home", component: Home }, { path: "/About", component: About } ]})
在main.js里使用路由器
//引入路由器import router from "./router"...// 创建Vue实例对象 new Vue({ render: h => h(App), router//使用路由器}).$mount("#app")
active-class
可以配置高亮样式:
About Home
pages
文件夹,一般组件通常放在components
文件夹。$route
属性,里面存储着自己的路由信息。router
,可以通过组件的$router
属性获取到。配置路由规则,使用children
配置项
routes: [ { path: "/Home", component: Home, children: [//通过children配置子级路由 { path: "news", //路由匹配子级时会自动加/,所以这里不能写/news component: News }, { path: "message", component: Message } ] } ]
News
{{ item.title }} {{ item.title }}
$route.query.id$route.query.title
在某些情况可以简化路由跳转的代码。
{path: "home",component: Home,children: [path: "message",component: Message,children: [{name: "Detail",//给路由命名path: "Detail",component: Detail}]]}
跳转 跳转
{ path: "/Home", component: Home, children: [ { path: "news", component: News }, { path: "message", component: Message, children: [ { name: "Detail", path: "Detail/:id/:title", //:xxx占位符,用于parmas传参 component: Detail } ] } ] }
{{ item.title }} {{ item.title }}
作用: 让路由组件更方便的收到参数。
////值为对象,该对象中所有key-value都会以props的形式传给Detail组件props: {a: 1,b: "hello"}
//值为布尔值,若布尔值为真,就会把该路由组件收到的pramas参数以props的形式传给Detail组件props: true
//值为函数,该函数返回的对象种每一组key-value都会通过props传给Detail组件props($route) {return {id: $route.query.id,title: $route.query.title}}
控制路由跳转时操作浏览器历史记录的模式。浏览器的历史有两种写入方式:
push
:追加记录。replace
:替换当前记录(不是所有记录,而是当前记录)。路由跳转默认为push
。w
不借助
实现路由跳转,让路由跳转更加灵活
//push方法跳转this.$router.push({name: "Detail",query: {id: item.id,title: item.title}})//replace方法跳转this.$router.replace({name: "Detail",query: {id: item.id,title: item.title}})//前进this.$router.forward()//后退this.$router.back()//前进n步this.$router.go(n)
让不展示的路由组件保持挂载,不被销毁。
路由组件所独有的两个钩子,用于捕获路由组件的激活状态。
activated(){...},//路由组件被激活时调用deactivated(){...}//路由组件失活时调用
对所有路由进行权限控制。
初始化时和每次路由切换之前执行。
// to目的地, from始发地, next到达执行//必须执行next()才能实现路由跳转router.beforeEach((to, from, next) => { console.log("前置",to, from); if(to.meta.isAuth) {//判断是否需要鉴定权限 if(localStorage.getItem("school") === "cloud") { next()//放行 }else { alert("学校名称不对,无权查看") } }else { next()//放行 }})
路由切换之后执行。
router.afterEach((to, from) => { console.log("后置",to, from); //默认title是cloud,路由跳转后根据路由相应的title更改 document.title = to.meta.title || "cloud"//修改网页的title})
对某个路由进行权限控制。只有独享前置守卫,没有独享后置守卫,可以与全局后置守卫配合使用。
beforeEnter(to, from, next) {console.log("独享", to, from);if (to.meta.isAuth) {//判断是否需要鉴定权限if (localStorage.getItem("school") === "cloud") {next()}else alert("学校名称不对,无权查看")}else next()}
beforeRouteEnter(to, from, next) {...}
beforeRouteLeave(to, from, next) {...}
对于一个url
来说,什么是hash
值?
#
后面的内容就是hash
值。hash
值不会包含在HTTP
请求当中,即: hash
值不会带给服务器。
#
号,不美观。修改模式在路由配置中添加mode
属性,默认值是hash
。
mode: "history"
标签: