1. nanoid:随机数
1.1安装:npm i nanoid
1.2引入:import {nanoid} from ‘nanoid’
1.3使用:nanoid()
2. 父子组件传值
2.1父组件声明与发送:父组件声明的方法或者数据,子组件可以直接使用
子组件声明接收:
子组件传递给孙组件:
孙组件接受使用:
2.2自定义事件传值(适用于子组件传给父组件)(用@或v-on):
父组件定义和绑定事件
子组件使用自定义事件并传值:
2.3 ref属性:可实现更多功能
3. filter过滤不改变原来的数据,类似for循环过滤
4.JSON
JSON.parse(str) 把json字符串转为对象;
JSON.stringify() 把对象转为json字符串
5.全局事件总线
定义总线
使用总线的自定义事件,并在对象销毁前关闭事件
在main.js里创建总线
//创建vm
new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this //安装全局事件总线
},
})
6.消息订阅
1.安装:npm i pubsub-js
2.引入:import pubsub from ‘pubsub-js’
3.发布:pubsub.publish(‘消息名称’,data)
4.订阅:mounted生命周期里:this.pubId = pubsub.subscribe(‘消息名称’,function)
5(注):function( ‘消息名称’, data) 或使用占位符 unction( _, data)
6.取消订阅:beforeDestroy生命周期里:pubsub.unsubscribe(this.pubId)
7.动画库
1.官网:https://animate.style/
2.安装:npm install animate.css
3.引入:在script标签中引入 import ‘animate.css’;
4.使用:在transition或transition-group标签中使用 name=“animate__animated animate__bounce”,再加上进入和离开的属性。(transition-group需要在子标签中指定key值)例如:
<transition-group
appear
name="animate__animated animate__bounce"
enter-active-class="animate__swing"
leave-active-class="animate__backOutUp"
>
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">尚硅谷!</h1>
</transition-group>
8.代理服务器
用于解决跨域问题:在vue.config.js中配置
devServer: {
proxy: {
'/demo': {
target: 'http://localhost:5001',
pathRewrite:{'^/demo':''},
// ws: true, //用于支持websocket
// changeOrigin: true //用于控制请求头中的host值
}
}
}
9.插槽
默认插槽:
1.父组件插入
<Category title="美食" >
<img src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
</Category>
2.子组件定义使用
<div class="category">
<h3>{{title}}分类</h3>
<!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充) -->
<slot>我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>
</div>
具名插槽:
1.父组件插入
<Category title="美食" >
<img slot="center" src="https://s3.ax1x.com/2021/01/16/srJlq0.jpg" alt="">
<a slot="footer" href="http://www.atguigu.com">更多美食</a>
</Category>
2.子组件定义使用
<div class="category">
<h3>{{title}}分类</h3>
<!-- 定义一个插槽(挖个坑,等着组件的使用者进行填充) -->
<slot name="center">我是一些默认值,当使用者没有传递具体结构时,我会出现1</slot>
</div>
作用域插槽:(可以子组件向父组件传值)
1.父组件插入
<Category title="游戏">
<template scope="atguigu">
<ul>
<li v-for="(g,index) in atguigu.games" :key="index">{{g}}</li>
</ul>
</template>
</Category>
2.子组件定义使用
<div class="category">
<h3>{{title}}分类</h3>
<slot :games="games" msg="hello">我是默认的一些内容</slot>
</div>
<script>
export default {
name:'Category',
props:['title'],
data() {
return {
games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
}
},
}
</script>
10.路由
1.配置路由,包含多级路由
1.1:在main.js中引入VueRouter:import VueRouter from ‘vue-router’
应用插件:Vue.use(VueRouter)
1.2:在src下创建router目录,router下创建index.js文件:
// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
//引入组件
import About from '../components/About'
import Home from '../components/Home'
//创建并暴露一个路由器
export default new VueRouter({
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
//二级路由
children:[
{
path:'news',
component:News,
},
{
path:'message',
component:Message,
//三级路由
children:[
{
path:'detail',
component:Detail,
}
]
}
]
}
]
})
2.使用路由
2.1:router-link转为 a标签
<!-- Vue中借助router-link标签实现路由的切换 -->
<router-link class="list-group-item" active-class="active" to="/about">About</router-link>
<router-link class="list-group-item" active-class="active" to="/home">Home</router-link>
<!-- 指定组件的呈现位置 -->
<router-view></router-view>
2.2:to根据组件path,例如to=“/home/news/detail”;
或使用对象方式,并携带query参数(同理可以使用params):
<router-link :to="{
path:'/home/message/detail',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
2.3:接受使用query参数
<li>消息编号:{{$route.query.id}}</li>
<li>消息标题:{{$route.query.title}}</li>
也可以用props来接收:props:['id','title'],
<li>消息编号:{{id}}</li>
<li>消息标题:{{title}}</li>
2.4:命名路由
children:[
{
name:'xiangqing',
path:'detail',
component:Detail,
}
]
<!-- 跳转路由并携带query参数,to的对象写法 -->
<router-link :to="{
name:'xiangqing',
query:{
id:m.id,
title:m.title
}
}">
2.5:<router-link>
的replace属性
- 浏览器的历史记录有两种写入方式:分别为
push
和replace
,push
是追加历史记录,replace
是替换当前记录。路由跳转时候默认为push
- 使用:
<router-link replace .......>News</router-link>
2.6:编程式路由导航
<button @click="pushShow(m)">push查看</button>
pushShow(m){
this.$router.push({
name:'xiangqing',
query:{
id:m.id,
title:m.title
}
})
},
还可以使用
this.$router.forward() //前进
this.$router.back() //后退
this.$router.go(number) //根据number可前进也可后退
2.7:缓存路由组件,让不展示的路由组件保持挂载,不被销毁。
不指明组件则会挂载全部
<keep-alive include="News">
<router-view></router-view>
</keep-alive>
2.8:两个新的生命周期钩子
- 作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态。
- 具体名字:
activated
路由组件被激活时触发,类似mounted。deactivated
路由组件失活时触发,类似beforeDestroy。
2.9:全局守卫
在配置路由时设置mate属性,例如在mate中设置 isAuth 属性
routes:[
{
name:'guanyu',
path:'/about',
component:About,
meta:{isAuth:true,title:'关于'}
},
配置路由守卫
//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to,from,next)=>{
console.log('前置路由守卫',to,from)
if(to.meta.isAuth){ //判断是否需要鉴权
//需要鉴权后,信息校验
if(localStorage.getItem('school')==='atguigu'){
next()
}else{
alert('学校名不对,无权限查看!')
}
}else{
next()
}
})
//全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
console.log('后置路由守卫',to,from)
//更改html标题title
document.title = to.meta.title || '硅谷系统'
})
2.10:独享路由守卫:在配置路由中使用beforeEnter方法,可配合全局路由守卫
(与name、meta同级)
beforeEnter: (to, from, next) => {
console.log('独享路由守卫',to,from)
if(to.meta.isAuth){ //判断是否需要鉴权
if(localStorage.getItem('school')==='atguigu'){
next()
}else{
alert('学校名不对,无权限查看!')
}
}else{
next()
}
}
2.11:组件内路由守卫:在组件中使用beforeRouteEnter、beforeRouteLeave方法,必须通过路由规则进入组件,才能调用方法,也可配合全局路由守卫(与全局路由守卫beforeEach、afterEach方法作用不同)
(与mounted同级)
//通过路由规则,进入该组件时被调用
beforeRouteEnter (to, from, next) {
console.log('About--beforeRouteEnter',to,from)
if(to.meta.isAuth){ //判断是否需要鉴权
if(localStorage.getItem('school')==='atguigu'){
next()
}else{
alert('学校名不对,无权限查看!')
}
}else{
next()
}
},
//通过路由规则,离开该组件时被调用
beforeRouteLeave (to, from, next) {
console.log('About--beforeRouteLeave',to,from)
next()
}
2.12:history模式与hash模式。
hash模式url中#后的数据不会传给后端
history模式没有#,会全部传给后端
mode:'hash' //默认hash
或者
mode:'history',
11.其他
v-bind:
可以缩写为:
v-on:click
可以缩写为@click
v-for
后需要加上:key
例如:v-for="(item,i) in items" :key="item.id"