vue使用動態(tài)組件手寫Router View實現(xiàn)示例
更新時間:2023年06月16日 09:22:13 作者:XiaoSong
這篇文章主要為大家介紹了vue使用動態(tài)組件手寫RouterView實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
使用動態(tài)組件實現(xiàn)一個簡單的<RouterView/>組件
1、創(chuàng)建view/Home.vue和view/About.vue
<!--?About?--> <template> ????about </template> <script?setup></script> <!--?Home?--> <template> ????home </template> <script?setup></script>
2、創(chuàng)建一個Router/index.js文件
內(nèi)容如下:
import?About?from?'../view/About.vue' import?Home?from?'../view/Home.vue' export?default?[ ????{ ????????path:?'/', ????????component:?Home ????}, ????{ ????????path:?'/about', ????????component:?About ????} ]
3、創(chuàng)建component/RouterView.vue
內(nèi)容如下
<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>
在瀏覽器地址框輸入對應(yīng)的path就能實現(xiàn)頁面的切換了。
以上就是vue使用動態(tài)組件手寫RouterView實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于vue手寫RouterView組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue?this.$router六種方法使用示例總結(jié)分析
這篇文章主要為大家介紹了vue this.$router六種方法使用示例總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06詳解Vue前端生產(chǎn)環(huán)境發(fā)布配置實戰(zhàn)篇
這篇文章主要介紹了詳解Vue前端生產(chǎn)環(huán)境發(fā)布配置實戰(zhàn)篇,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05vue element-ui里的table中表頭與表格出現(xiàn)錯位的解決
這篇文章主要介紹了vue element-ui里的table中表頭與表格出現(xiàn)錯位的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08使用Vue-Office實現(xiàn)Office文件預(yù)覽組件
在現(xiàn)代Web應(yīng)用中,Office 文件的預(yù)覽是一個常見的需求,本文將使用Vue-Office實現(xiàn)Office文件預(yù)覽組件,感興趣的小伙伴可以跟隨小編一起學習一下2025-03-03