详解Vue2基本语法第五弹

全局事件总线

1、一种组件间通信的方式,适用于任意组件间通信

2、安装全局事件总线:

new Vue({
...
beforeCreate(){
    Vue.prototype.$bus = this//安装事件总线,$bus就是当前应用的vm
    }
})

3、使用事件总线:

(1)接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件自身

methods(){
    demo(data){}
    ...
    mounted(){
        this.$bus.$on("xxx",this.demo)
    }
}

(2)提供数据:

this.$bus.$emit("xxx",数据)

4、最好在`beforeDestroy钩子中,用$off去解绑当前组件所用到的事件

消息订阅

1、一种组件间通信的方式,适用于任意组件间通信

2、使用步骤:

(1)安装pubsub:

npm i pubsub-js

(2)引入:

import pubsub from "pubsub-js"

(3)接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身

methods(){
    demo(data){...}
    mounted(){
        this.pid = pubsub.subscribe("xxx",this.demo)//订阅消息
    }   
}

4、提供数据:

pubsub.publish("xxx",数据)

5、最好在beforeDestroy构子中,用Pubsub.unscrible(pid)去取消订阅

nextTick

1、语法:

this.$nextTick(回调函数)

2、作用:在下一次DOM更新结束后执行其指定的回调

3、什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行

Vue封装的过渡与动画

1、作用:在插入、更新或移除DOM元素时,在合适的时候添加样式类名

2、图示:

Transition Diagram

3、写法:

(1)准备好样式:

元素进入的样式:

  1. v-enter:进入的起点

  2. v-enter-active:进入过程中

  3. v-enter-to:进入的终点

元素离开的样式:

  1. v-leave:离开的起点

  2. v-leave-active:离开过程中

  3. v-leave-to:离开的终点

(2)使用<transition>包裹要过渡的元素,要配置name属性:

<transition name = "xxx" appear>
//可以通过 appear 特性设置节点的在初始渲染的过渡
    <h1>。。。</h1>
</transition>

3、备注:若有多个元素需要过渡,则需要使用:<trasition-group>,且每个元素都要指定key

vue脚手架配置代理

方法一:

在vue.config.js中添加如下配置:

devServer:{
    proxy:"http://localhost:5000"
}

说明:

1、优点:配置简单,请求资源直接发给前端(8080)即可

2、缺点:不能配置多个代理,不能灵活的控制请求是否走代理

3、工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器(优先匹配前端资源)

方法二:

编写vue.config.js配置具体代理规则:

devServer:{
    proxy:{
        "/api1":{//匹配所有/api1开头的请求路径
            target:"http://localhost:5000",//代理目标的基础路径
            changeOrigin:true,
            pathRewrite:{"^/api1":""}
        }
    }
}
//changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000
//changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080
//changeOrigin默认值为true
​

说明:

1、优点:可以配置多个代理,且可以灵活的控制请求是否走代理

2、缺点:配置略微繁琐,请求资源时必须加前缀

插槽

1、作用:让父组件可以向子组件指定位置插入HTML结构,也是一种组件间通信的方式,适用于父组件 ===> 子组件

2、分类:默认插槽、具名插槽、作用域插槽

3、使用方式:

默认插槽

父组件中:

<Category>
    <div>html结构</div>
</Category>

子组件中:

<template>
    <div>
        <!--定义插槽 -->
        <slot> 插槽默认内容</slot>
    </div>
</template>

具名插槽

父组件中:

<Category>
    <template slot = "center">
        <div>html结构</div>
    </template>
    
    <template v-slot = footer>
        <div>html结构</div>
    </template>
</Category>

子组件中:

<template>
    <div>
        <!--定义插槽 -->
        <slot name = "center"> 插槽默认内容</slot>
        <slot name = "footer"> 插槽默认内容</slot>
    </div>
</template>

作用域插槽

1、理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者决定。

2、具体编码:父组件中:

<Category>
    <template scope = "scopeData">//使用scope接收数据
        <ul>
            <li>...</li>
        </ul>
    </template>
   
</Category>

子组件中:

<template>
    <div>
        <!--定义插槽 -->
        <slot :games = "games"> 插槽默认内容</slot><!--传递数据-->
    </div>
</template>
​
<script>
export default {
        name:'Category',
        props:['title'],
        //数据在子组件自身
        data() {
            return {
                games:['红色警戒','穿越火线','劲舞团','超级玛丽']
            }
        },
    }
</script>

路由

1、理解:一个路由(route)就是一组映射关系(key-value),多个路由需要路由器(router)进行管理

2、前端路由:key是路径,value是组件

基本使用

1、安装vue-router,命令:

npm i vue-router

2、应用插件:

Vue.use(VueRouter)

3、编写router配置项:

import VueRouter from "vue-router"
import About from "..."
import Home from "..."
const router = new VueRouter({
    routes:[{
        path:"./about",
        component:About
        },
        {
        path:"./home",
        component:Home
            }
    ]
})
//暴露router
export default router

4、实现切换(active-class可配置高亮样式)

<router-link active-class="active" to="/about">About</router-link>

5、指定展示位置

<router-view></router-view>

​几个注意点

1、路由组件通常存放在pages文件夹,一般组件通常存放在components文件夹

2、通过切换,隐藏了的路由组建默认是被销毁掉的,需要的时候再去挂载

3、每个组件都有自己的route属性,里面存储着自己的路由信息

4、整个应用只有一个router,可以通过组件里的router属性获取到

多级路由

1、配置路由规则,使用children配置项

routes:[
    {
    path:"./about",
    component:About
    },
    {
    path:"./home",
    component:Home,
    children:[
        {
            path:"news",
            component:News
        },
        {
            path:"message",
            component:Message
        }
    ]
    }
]

2、跳转(要写完整路径)

<router-link to="/home/news">News</router-link>

​路由的query参数

1、传递参数

//跳转并携带query参数,to的字符串写法
<router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link>
//跳转并携带query参数,to的字符串写法
<router-link
    :to="{
        path:"/home/message/detail",
        query:{
            id:666,
            title:"你好"
        }
    }"
>跳转</router-link>

2、接收参数

$router.query.id
$router.query.title

​命名路由

1、作用:可以简化路由的跳转

2、如何使用:

(1)给路由命名:

{
path:'/demo',
component:Demo,
children:[
    {
        path:'test',
        component:Test,
        children:[
                    {
                        name:'hello' //给路由命名
                        path:'welcome',
                        component:Hello,
                    }
                ]
          }
    ]
}

(2)简化跳转

//简化后,直接通过名字跳转
<router-link :to="{name:'hello'}">跳转</router-link>
//简化后写法配合传递参数
<router-link
    :to="{
        name:"hello",
        query:{
            id:666,
            title:"你好"
        }
    }"
>跳转</router-link>

​路由的params参数

​1、配置路由,声明接收params参数

 {
    path:'/home',
    component:Home,
    children:[
        {
            path:'news',
            component:News
        },
        {
            component:Message,
            children:[
                {
                    name:'xiangqing',
                    path:'detail/:id/:title', //使用占位符声明接收params参数
                    component:Detail
                }
            ]
        }
    ]
   }

2、传递参数

//跳转并携带params参数,to的字符串写法
<router-link :to="/home/message/detail/666/你好">跳转</router-link>
//跳转并携带params参数,to的对象写法
<router-link
    :to="{
        name:"xiangqing",
        params:{
            id:666,
            title:"你好"
        }
    }"
>跳转</router-link>

注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置项

3、接收参数

$routr.params.id
$route.params.title

​路由的props配置

作用:让路由组件更方便接收到参数

{
    name:"xiangqing",
    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:routr.query.title
        }
    }
}

​replace属性

1、作用:控制路由跳转时操作浏览器里历史记录的模式

2、浏览器的历史记录有两种写入方式,分别为pushreplacepush是追加历史记录,replace是替换当前记录,路由跳转默认为push

3、如何开启replace模式:

<router-link replace ...>News</router-link>

​编程式路由导航

1、作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活

2、具体编码:

$router的两个API
this.$router.push({
    name:"xiangqing",
    params:{
        id:xxx,
        title:xxx
    }
})
this.$router.replace({
    name:'xaingqing',
    paarams:{
        id:xxx,
        title:xxx
    }
})
this.$router.forward()//前进
this.$router.back()//后退
this.$router.go()//可前进可后退

​缓存路由组件

1、作用:让不展示的路由组件保持挂载,不被销毁

2、具体编码:

<keep-alive>
    <router-view></router-view>
</keep-alive>

​两个新的生命周期构子

1、作用:路由组件中所独有的构子,用于捕获路由组件的激活状态

2、具体名字:

  • activated:路由组件被激活时触发

  • deactivated:路由组件失活时触发

路由守卫

1、作用:对路由进行权限控制

2、分类:全局守卫,独享守卫,组件内守卫

3、全局守卫

//全局前置守卫:初始化时执行,每次路由切换执行
router.beforeEach((to,from,next) => {
    if(to.meta.isAuth){//判断当前路由是否需要进行权限控制
        if(localStorage.getItem("school")==="atguigu"){
            next()//放行
        }else{
            alert("xxx")
        }
    }else{
        next()
    }
})
//全局后置守卫,初始化时执行,每次路由切换后执行
router.afterEach((to,from) => {
    if(to.meta.title){
        document.title = to.meta.title
    }else{
        document.title = "vue_test"
    }
})

4、独享路由

beforeEnter(to,from,next){
    if(to.meta.isAuto){
        if(localStorage.getItem("school") === "atguigu"){
            next()
        }else{
            alert("xxx")
        }
    }else{
        next()
    }
}

5、组件内守卫

//进入守卫:通过路由规则,进入该组件时被调用
beforeRouteEnter(to,from,next){
}
//离开守卫:通过路由规则,离开该组件时被调用
beforeRouteLeave(to,from,next){
}

​路由器的两种工作模式

1、对于一个url来说,什么是hash值? #及其后面的内容就是hash值

2、hash值不会包含在HTTP请求中,即hash值不会带给服务器

3、hash模式:

(1)地址中永远带着#号,不美观

(2)若以后将地址通过第三方手机APP分享,若APP校验严格,则地址会被标记为不合法

(3)兼容性较好

4、history模式: (1)地址干净

(2)兼容性与hash模式相比较差

(3)应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值