vue3中頁面跳轉(zhuǎn)兩種實現(xiàn)方式
z1. 普通函數(shù)的頁面跳轉(zhuǎn)
<template> <router-view></router-view> <router-link to="/">首頁</router-link> <button @click="go">我的</button> </template> <script setup> import { useRouter } from 'vue-router' // 使用 useRouter 創(chuàng)建一個 router 實例 const router = useRouter() // 定義 go 函數(shù)以便路由跳轉(zhuǎn) function go() { router.push({ path: '/my' }) } // 將 go 函數(shù)暴露給模板 </script>
2.箭頭函數(shù)的實現(xiàn)方法
<template> <router-view></router-view> <router-link to="/">首頁</router-link> <button @click="go">我的</button> </template> <script setup> import { useRouter } from 'vue-router' // 使用 useRouter 創(chuàng)建一個 router 實例 const router = useRouter() // 定義 go 函數(shù)以便路由跳轉(zhuǎn) const go = () => { router.push('/my') } </script>
在router 文件夾下的 index.vue文件添加 ' /my ' 路由
{ path: '/my', component: () => import('@/views/my'), hidden: true },
在 permission.js白名單文件下 添加 '/my'
const whiteList = ['/login', '/register', '/forget_pwd', '/index', '/my']
注 :下面是 Vue Router 的組件,用于處理 Vue 應(yīng)用中的路由功能。它們通常用于定義路由的視圖和鏈接。
- <router-view> 是一個用于展示匹配路由組件的占位符。
- <一> 嵌套路由
1. 路由路徑和組件的關(guān)系
在 Vue 應(yīng)用中,路由路徑(例如 /home
、/about
)與組件(例如 HomeComponent
、AboutComponent
)是關(guān)聯(lián)的。
const routes = [ { path: '/', component: HomeComponent }, { path: '/about', component: AboutComponent } ]
2. 路由視圖占位符
<router-view> 是一個占位符,用于告訴Vue Router 在這個位置渲染當前路由匹配的組件??梢园阉斫鉃橐粋€動態(tài)的容器,負責顯示與當前路徑相匹配的組件。
3. 路由視圖占位符
當用戶在瀏覽器中導航到不同的路徑時。Vue Router 會根據(jù)路由配置找到對應(yīng)的組件,并將這些組件渲染到<router-view> 中 。列如:
當用戶訪問 / 路徑時,HomeComponent 會被渲染到 <router-view> 位置
當用戶訪問 /about 路徑時,AboutComponent 會被渲染到 <router-view> 位置
4. 示例
假設(shè)你有一個簡單的Vue 應(yīng)用,有兩個頁面:主頁面和關(guān)于頁。路由配置如下
const routes = [ { path: '/', component: HomeComponent }, { path: '/about', component: AboutComponent } ]
<template> <div> <router-view></router-view> </div> </template>
當用戶訪問 / 路徑時,HomeComponent 會被渲染到 <router-view> 位置
<!-- HomeComponent --> <template> <div> <h1>Home Page</h1> </div> </template>
當用戶訪問 /about 路徑時,AboutComponent 會被渲染到 <router-view> 位置
<!-- AboutComponent --> <template> <div> <h1>About Page</h1> </div> </template>
<二> 命名視圖 :允許你在一個頁面中同時展示多個 <router-view>
,每個 <router-view>
可以渲染不同的組件。
1.當用戶訪問路徑 /dashboard 時,vue router 會同時渲染所有與該路徑相關(guān)的組件到各自的命名視圖中。所有的<router-view>組件會同時被填充內(nèi)容,而不是按某種順序或條件來渲染。
2. 假設(shè)有以下路由配置
// router/index.js import { createRouter, createWebHistory } from 'vue-router' import HeaderComponent from '../components/HeaderComponent.vue' import MainComponent from '../components/MainComponent.vue' import FooterComponent from '../components/FooterComponent.vue' const routes = [ { path: '/dashboard', components: { header: HeaderComponent, main: MainComponent, footer: FooterComponent } } ] const router = createRouter({ history: createWebHistory(), routes }) export default router
以及對應(yīng)的Vue組件模板
<!-- App.vue --> <template> <div> <router-view name="header"></router-view> <router-view name="main"></router-view> <router-view name="footer"></router-view> </div> </template>
HeaderComponent.vue:
<template> <header> <h1>Header</h1> </header> </template>
MainComponent.vue:
<template> <main> <h2>Main Content</h2> </main> </template>
FooterComponent.vue:
<template> <footer> <p>Footer</p> </footer> </template>
<router-link> 是一個用于生成導航鏈接的組件,使得用戶可以通過點擊鏈接來改變當前的路由。
總結(jié)
到此這篇關(guān)于vue3中頁面跳轉(zhuǎn)兩種實現(xiàn)方式的文章就介紹到這了,更多相關(guān)vue3頁面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pinia進階setup函數(shù)式寫法封裝到企業(yè)項目
這篇文章主要為大家介紹了Pinia進階setup函數(shù)式寫法封裝到企業(yè)項目實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07vue+koa2實現(xiàn)session、token登陸狀態(tài)驗證的示例
這篇文章主要介紹了vue+koa2實現(xiàn)session、token登陸狀態(tài)驗證的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08vue-devtools?開發(fā)工具插件之支持vue3?chrome?瀏覽器插件
這篇文章主要介紹了vue-devtools?開發(fā)工具插件之支持vue3?chrome?瀏覽器插件,用這個版本主要是為了支持vue3?推薦直接下載,文中給大家提供了下載地址,感興趣的朋友跟隨小編一起看看吧2022-01-01axios發(fā)送post請求,提交圖片類型表單數(shù)據(jù)方法
下面小編就為大家分享一篇axios發(fā)送post請求,提交圖片類型表單數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue3實現(xiàn)刷新頁面局部內(nèi)容的示例代碼
本文主要介紹了Vue3實現(xiàn)刷新頁面局部內(nèi)容的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07