vue使用动态组件手写Router View实现示例
更新时间:2023年06月16日 09:22:13 作者:XiaoSong
这篇文章主要为大家介绍了vue使用动态组件手写RouterView实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
使用动态组件实现一个简单的<RouterView/>组件
1、创建view/Home.vue和view/About.vue
<!-- About --> <template> about </template> <script setup></script> <!-- Home --> <template> home </template> <script setup></script>
2、创建一个Router/index.js文件
内容如下:
import About from '../view/About.vue'
import Home from '../view/Home.vue'
export default [
{
path: '/',
component: Home
},
{
path: '/about',
component: About
}
]3、创建component/RouterView.vue
内容如下
<template>
<div>
<component :is="view"></component>
</div>
</template>
<script setup>
import { computed } from 'vue'
import Router from '../Router';
const view = computed(() => {
const res = Router.find(item => item.path == window.location.pathname)
return res.component
})
</script>
<style lang="scss" scoped>
</style>4、在App.vue中引入自己写的RouterView组件
<script setup> import RouterView from './components/RouterView.vue'; </script> <template> <div> <RouterView></RouterView> </div> </template>
在浏览器地址框输入对应的path就能实现页面的切换了。
以上就是vue使用动态组件手写RouterView实现示例的详细内容,更多关于vue手写RouterView组件的资料请关注脚本之家其它相关文章!
相关文章
ElementUI $notify通知方法中渲染自定义组件实现
这篇文章主要为大家介绍了ElementUI $notify通知方法中渲染自定义组件实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-06-06
一文解决vue2 element el-table自适应高度问题
在写公司后台项目的时候遇到一个需求,要求表格页面不能有滚动条,所以必须封装一个公共方法来实现表格自适应高度,本问小编给大家介绍了如何解决vue2 element el-table自适应高度问题,需要的朋友可以参考下2023-11-11
超详细动手搭建一个VuePress 站点及开启PWA与自动部署的方法
这篇文章主要介绍了超详细动手搭建一个VuePress 站点及开启PWA与自动部署的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-01-01


最新评论