感兴趣的朋友可以去我的语雀平台进行查看更多的知识。
https://www.yuque.com/ambition-bcpii/muziteng
7.8 路由的props配置
props
作用:让路由组件更方便的收到参数
{name:'detail',path:'detail/:id',component:Detail,//第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件// props:{a:900}//第二种写法:props值为布尔值,为true时,则把路由收到的所有params参数通过props传给Detail组件// props:true//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件props($route){return {id: $route.query.id,title: $route.query.title}}
}
实例:
src/router/index.js
// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
export default new VueRouter({routes: [{path: '/home',component: Home,children: [{path: "news",component: News,},{path: "message",component: Message,children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title", // 使用占位符声明接受params参数component: Detail,// props的第一种写法,值为对象,该对象中的key-value都会以props的形式传给Detail组件// props: {a: 1, b: 'hello'}// props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的params参数,以props的形式传给Detail组件// props: true,// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{path: '/about',component: About,}]
})
src/pages/Message.vue
<template><div><ul><li v-for="m in messageList" :key="m.id"><!--跳转并携带params参数,to的对象写法(推荐)--><router-link:to="{name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}}">{{ m.title }}</router-link></li></ul><hr/><router-view></router-view></div>
</template><script>
export default {name: "Message",data() {return {messageList: [{id: '001', title: '消息001'},{id: '002', title: '消息002'},{id: '003', title: '消息003'},]}}
}
</script>
src/pages/Detail.vue
<template><ul><li>消息编号:{{ id }}</li><li>消息标题:{{ title }}</li></ul>
</template><script>
export default {name: "Detail",props: ['id', 'title']
}
</script>
7.9 路由跳转的replace方法
- 作用:控制路由跳转时操作浏览器历史记录的模式
- 浏览器的历史记录有两种写入方式:push和replace
push
是追加历史记录
replace
是替换当前记录,路由跳转时候默认为push
方式 - 开启replace模式
<router-link :replace="true" ...>News</router-link>
简写<router-link replace ...>News</router-link>
- 总结:浏览记录本质是一个栈,默认
push
,点开新页面就会在栈顶追加一个地址,后退,栈顶指针向下移动,改为replace
就是不追加,而将栈顶地址替换
src/App.vue
<template><div><Banner/><div class="row"><div class="col-xs-2 col-xs-offset-2"><div class="list-group"><!-- Vue中借助router-link标签实现路由的切换 --><router-link replace class="list-group-item" active-class="active" to="/about">About</router-link><router-link replace class="list-group-item" active-class="active" to="/home">Home</router-link></div></div><div class="col-xs-6"><div class="panel"><div class="panel-body"><!-- 指定组件的呈现位置 --><router-view></router-view></div></div></div></div></div>
</template><script>
import Banner from "@/components/Banner";export default {name: 'App',components: {Banner},
}
</script>
7.10 编程式路由导航(不用<router-link>
)
作用:不借助<router-link>
实现路由跳转,让路由跳转更加灵活
this.$router.push({})
内传的对象与<router-link>
中的to
相同
this.$router.replace({})
this.$router.forward()
前进
this.$router.back()
后退
this.$router.go(n)
可前进也可后退,n为正数前进n,为负数后退
this.$router.push({name:'detail',params:{id:xxx,title:xxx}
})this.$router.replace({name:'detail',params:{id:xxx,title:xxx}
})
实例:
src/components/Banner.vue
<template><div class="row"><div class="col-xs-offset-2 col-xs-8"><div class="page-header"><h2>Vue Router Demo</h2><button @click="back">后退</button><button @click="forward">前进</button></div></div></div>
</template><script>
export default {name: "Banner",methods: {back() {this.$router.back()},forward() {this.$router.forward()}}
}
</script>
src/pages/Message.vue
<template><div><ul><li v-for="m in messageList" :key="m.id"><!--跳转并携带params参数,to的对象写法(推荐)--><router-link:to="{name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}}">{{ m.title }}</router-link><button @click="pushShow(m)">push查看</button><button @click="replaceShow(m)">replace查看</button></li></ul><hr/><router-view></router-view></div>
</template><script>
export default {name: "Message",data() {return {messageList: [{id: '001', title: '消息001'},{id: '002', title: '消息002'},{id: '003', title: '消息003'},]}},methods: {pushShow(m) {this.$router.push({name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}})},replaceShow(m) {this.$router.replace({name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}})}}
}
</script>
7.11 缓存路由组件
作用:让不展示的路由组件保持挂载,不被销毁
<keep-alive include="News"><router-view></router-view></keep-alive>
<keep-alive :include="['News', 'Message']"><router-view></router-view></keep-alive>
// 缓存一个路由组件
<keep-alive include="News"> // include中写想要缓存的组件名,不写表示全部缓存<router-view></router-view>
</keep-alive>// 缓存多个路由组件
<keep-alive :include="['News','Message']"> <router-view></router-view>
</keep-alive>
实例:
src/pages/News.vue
<template><ul><li>news001 <input type="text"></li><li>news002 <input type="text"></li><li>news003 <input type="text"></li></ul>
</template><script>
export default {name: "News"
}
</script>
src/pages/Home.vue
<template><div><h2>我是Home的内容</h2><ul class="nav nav-tabs"><li><router-link class="list-group-item" active-class="active" to="/home/news">News</router-link></li><li><router-link class="list-group-item" active-class="active" to="/home/message">Message</router-link></li></ul><keep-alive include="News"><router-view></router-view></keep-alive></div>
</template><script>
export default {name: "Home",
}
</script>
7.12 activated deactivated
activated
和deactivated
是路由组件所独有的两个钩子,用于捕获路由组件的激活状态
具体使用
activated
路由组件被激活时触发deactivated
路由组件失活时触发
src/pages/News.vue
<template><ul><li :style="{opacity}">欢迎学习Vue</li><li>news001 <input type="text"></li><li>news002 <input type="text"></li><li>news003 <input type="text"></li></ul>
</template><script>
export default {name: "News",data() {return {opacity: 1,}},activated() {console.log("News组件被激活了")this.timer = setInterval(() => {this.opacity -= 0.01;if (this.opacity <= 0) this.opacity = 1}, 16);},deactivated() {console.log("News组件失活了")clearInterval(this.timer)}
}
</script>
7.13 路由守卫
作用:对路由进行权限控制
分类:全局守卫、独享守卫、组件内守卫
7.13.1 全局路由守卫
meta
路由元信息
// 全局前置守卫:初始化时、每次路由切换前执行
router.beforeEach((to,from,next) => {console.log('beforeEach',to,from)if(to.meta.isAuth){ // 判断当前路由是否需要进行权限控制if(localStorage.getItem('school') === 'teng'){ // 权限控制的具体规则next() // 放行}else{alert('暂无权限查看')}}else{next() // 放行}
})// 全局后置守卫:初始化时、每次路由切换后执行
router.afterEach((to,from) => {console.log('afterEach',to,from)if(to.meta.title){ document.title = to.meta.title //修改网页的title}else{document.title = 'vue_test'}
})
实例:
src/router/index.js
// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
const router = new VueRouter({routes: [{name: 'zhuye',path: '/home',meta: {title: '主页'},component: Home,children: [{name: 'xinwen',path: "news",component: News,meta: {isAuth: true, title: '新闻'}},{name: 'xiaoxi',path: "message",component: Message,meta: {isAuth: true, title: '消息'},children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title", // 使用占位符声明接受params参数component: Detail,meta: {isAuth: true, title: '详情'},// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{name: 'guanyu',path: '/about',component: About,meta: {title: '关于'}}]
})// 全局前置路由守卫---初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {console.log("前置路由守卫", to, from)// 判断是否需要鉴权if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}
})// 全局后置路由守卫---初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {console.log("后置路由守卫", to, from)document.title = to.meta.title || 'Vue学习'
})export default router;
7.13.2 独享路由守卫
beforeEnter(to,from,next){console.log('beforeEnter',to,from)if(localStorage.getItem('school') === 'atguigu'){next()}else{alert('暂无权限查看')}
}
实例:
src/router/index.js
// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
const router = new VueRouter({routes: [{name: 'zhuye',path: '/home',meta: {title: '主页'},component: Home,children: [{name: 'xinwen',path: "news",component: News,meta: {isAuth: true, title: '新闻'},// 独享守卫,特点路由切换之后被调用beforeEnter: (to, from, next) => {if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}}},{name: 'xiaoxi',path: "message",component: Message,meta: {isAuth: true, title: '消息'},children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title", // 使用占位符声明接受params参数component: Detail,meta: {isAuth: true, title: '详情'},// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{name: 'guanyu',path: '/about',component: About,meta: {title: '关于'}}]
})// 全局后置路由守卫---初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {console.log("后置路由守卫", to, from)document.title = to.meta.title || 'Vue学习'
})export default router;
7.13.3 组件内路由守卫
//进入守卫:通过路由规则,进入该组件时被调用
beforeRouteEnter (to, from, next) {... next()},//离开守卫:通过路由规则,离开该组件时被调用
beforeRouteLeave (to, from, next) {... next()},
实例:
src/pages/About.vue
<template><h2>我是About的内容</h2>
</template><script>
export default {name: "About",// 通过路由规则,进入该组件时被调用beforeRouterEnter(to, from, next) {console.log('About-beforeRouterEnter')if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}},// 通过路由规则,离开该组件时被调用beforeRouteLeave(to, from, next) {console.log('About-beforeRouteLeave')next()}
}
</script>
7.14 路由器的两种工作模式
-
对于一个
url
来说,什么是hash
值?#
及其后面的内容就是hash
值 -
hash
值不会包含在HTTP
请求中,即:hash
值不会带给服务器 -
hash
模式- 地址中永远带着
#
号,不美观 - 若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法
- 兼容性较好
- 地址中永远带着
-
history模式
- 地址干净,美观
- 兼容性和hash模式相比略差
- 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题
const router = new VueRouter({mode:'history',routes:[...]
})export default router
8. Vue UI组件库
8.1 常用的UI组件库
8.1.1 移动端常用UI组件库
-
Vant
-
Cube UI
-
Mint UI
-
https://nutui.jd.com/#/
8.1.2 PC端常用UI组件库
-
Element UI
-
IView UI
8.2 element-ui基本使用
- 安装
element-ui
:npm i element-ui -S
src/main.js
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'; // 引入ElementUI组件库
import 'element-ui/lib/theme-chalk/index.css'; // 引入ElementUI全部样式Vue.config.productionTip = falseVue.use(ElementUI) // 使用ElementUInew Vue({el:"#app",render: h => h(App),
})
src/App.vue
<template><div><br><el-row><el-button icon="el-icon-search" circle></el-button><el-button type="primary" icon="el-icon-edit" circle></el-button><el-button type="success" icon="el-icon-check" circle></el-button><el-button type="info" icon="el-icon-message" circle></el-button><el-button type="warning" icon="el-icon-star-off" circle></el-button><el-button type="danger" icon="el-icon-delete" circle></el-button></el-row></div>
</template><script>export default {name:'App',}
</script>
8.3 element-ui按需引入
- 安装
babel-plugin-component
npm i babel-plugin-component -D
- 修改
babel-config-js
module.exports = {presets: ['@vue/cli-plugin-babel/preset',["@babel/preset-env", { "modules": false }]],plugins: [["component",{ "libraryName": "element-ui","styleLibraryName": "theme-chalk"}]]
}
src/main.js
import Vue from 'vue'
import App from './App.vue'
import { Button,Row } from 'element-ui' // 按需引入Vue.config.productionTip = falseVue.component(Button.name, Button);
Vue.component(Row.name, Row);
/* 或写为* Vue.use(Button)* Vue.use(Row)*/new Vue({el:"#app",render: h => h(App),
})