实现简单vueRouter的封装

思路:1.点击按钮,实现 地址栏路径变化和组件的切换

2.地址栏上的前进和后退按钮,实现 地址栏路径变化和组件的切换

3.直接在url地址栏中输入path,实现 地址栏路径变化和组件的切换

  1. 点击按钮,实现 地址栏路径变化和组件的切换

<template>
  <div class="app-page">
    <div>
        <button @click="push('/home')">首页</button>
        <button @click="push('/message')">消息</button>
        <button @click="push('/mine')">我的</button>
    </div>
    <hr>
    <div>
        <!-- 使用component动态组件,实现路由出口 -->
        <component :is="componentId"></component>
    </div>
  </div>
</template>

<script setup lang='ts'>
import { shallowRef, ref } from "vue"
import Home from './components/Home.vue'
import Message from './components/Message.vue'
import Mine from './components/Mine.vue'
// 创建一个路由映射表
const routes = [
    { path: '/home', component: Home },
    { path: '/message',component: Message},
    { path: '/mine',component: Mine}
]

//这里不建议使用ref做响应式,vue组件本身深度较为复杂,使用ref,会导致层层劫持,造成性能浪费,我们只需要劫持组件第一层即可,所以推荐使用shallowRef
const componentId = shallowRef()

const push:(path:string)=>void = (path) => {
    // console.log(path);
    // 使用history上的pushState事件,改变url地址栏中的path
    history.pushState(null, '', path)
    // 查找对应组件,进行动态加载
    componentId.value = routes.find(item=>item.path===path)?.component
}

</script>

到这里,就实现了点击按钮的情况

  1. 地址栏上的前进和后退按钮,实现 地址栏路径变化和组件的切换

<template>
  <div class="app-page">
    <div>
        <button @click="push('/home')">首页</button>
        <button @click="push('/message')">消息</button>
        <button @click="push('/mine')">我的</button>
    </div>
    <hr>
    <div>
        <!-- 使用component动态组件,实现路由出口 -->
        <component :is="componentId"></component>
    </div>
  </div>
</template>

<script setup lang='ts'>
import { shallowRef, ref } from "vue"
import Home from './components/Home.vue'
import Message from './components/Message.vue'
import Mine from './components/Mine.vue'
// 创建一个路由映射表
const routes = [
    { path: '/home', component: Home },
    { path: '/message',component: Message},
    { path: '/mine',component: Mine}
]

//这里不建议使用ref做响应式,vue组件本身深度较为复杂,使用ref,会导致层层劫持,造成性能浪费,我们只需要劫持组件第一层即可,所以推荐使用shallowRef
const componentId = shallowRef()

const push:(path:string)=>void = (path) => {
    // console.log(path);
    // 使用history上的pushState事件,改变url地址栏中的path
    history.pushState(null, '', path)
    // 查找对应组件,进行动态加载
    componentId.value = routes.find(item=>item.path===path)?.component
}
//给windown注册popstate事件,实现地址栏中的前进后退按钮,实现 地址栏路径变化和组件的切换
window.onpopstate = function () {
    // console.log(location.pathname);
    // 查找对应组件,进行动态加载
    componentId.value = routes.find(item=>item.path===location.pathname)?.component
}

</script>
  1. 直接在url地址栏中输入path,实现 地址栏路径变化和组件的切换

<template>
  <div class="app-page">
    <div>
        <button @click="push('/home')">首页</button>
        <button @click="push('/message')">消息</button>
        <button @click="push('/mine')">我的</button>
    </div>
    <hr>
    <div>
        <!-- 使用component动态组件,实现路由出口 -->
        <component :is="componentId"></component>
    </div>
  </div>
</template>

<script setup lang='ts'>
import { shallowRef, ref,watch } from "vue"
import Home from './components/Home.vue'
import Message from './components/Message.vue'
import Mine from './components/Mine.vue'
// 创建一个路由映射表
const routes = [
    { path: '/home', component: Home },
    { path: '/message',component: Message},
    { path: '/mine',component: Mine}
]

//这里不建议使用ref做响应式,vue组件本身深度较为复杂,使用ref,会导致层层劫持,造成性能浪费,我们只需要劫持组件第一层即可,所以推荐使用shallowRef
const componentId = shallowRef()

const changeComponentId = (path: string) => {
    // 查找对应组件,进行动态加载
    componentId.value = routes.find(item=>item.path===path)?.component
}

const push:(path:string)=>void = (path) => {
    // console.log(path);
    // 使用history上的pushState事件,改变url地址栏中的path
    history.pushState(null, '', path)
    changeComponentId(path)
}
//给windown注册popstate事件,实现地址栏中的前进后退按钮,实现 地址栏路径变化和组件的切换
window.onpopstate = function () {
    // console.log(location.pathname);
    // 查找对应组件,进行动态加载
    changeComponentId(location.pathname)
}

// 监听location.pathname  实现组件切换
const herf = ref(location.pathname)
watch(herf, () => {
    changeComponentId(location.pathname)
}, {
    immediate:true
})

</script>

这样效果就实现啦!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值