這篇文章帶大家了解Vue3中路由,聊聊路由的基本設定、動態路由的設定、路由模式、路由重新導向等,希望對大家有幫助。
【相關推薦:《vue.js教學》】
路由的基本設定
#1、安裝外掛程式
npm install vue-router@next --save
2、建立一個routers.ts檔案
3、在routers.ts中引入元件並配置路徑。
import { createRouter,createWebHashHistory } from 'vue-router'; // 引入组件 import Home from './components/Home.vue'; import News from './components/News.vue'; import User from './components/User.vue'; const router = createRouter({ history: createWebHashHistory(), routes: [ {path: '/', component: Home}, {path: '/news', component: News}, {path: '/user', component: User}, ] }) export default router;
4、在main.ts中將路由檔案掛載到vue身上。
import { createApp } from 'vue' import App from './App.vue' import routers from './routers'; // createApp(App).mount('#app') const app = createApp(App); app.use(routers); app.mount('#app');
5、用到路由的元件通過router-view元件或router-link
<template> <img src="/static/imghwm/default1.png" data-src="./assets/logo.png" class="lazy" alt="Vue logo" > <ul> <li> <router-link to="/">首页</router-link> </li> <li> <router-link to="/news">新闻</router-link> </li> <li> <router-link to="/user">用户</router-link> </li> </ul> <router-view></router-view> </template>
掛載router-link後,只需要在元件對應的頁面路徑上輸入指定路由即可完成跳轉,router-link則實現a標籤進行跳轉的形式路由。
動態路由的設定
在routes.ts中依照下面的方式進行設定路由,透過/:aid的方式來進行動態路由的設定。
//配置路由 const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Home , alias: '/home' }, { path: '/news', component: News }, { path: '/user', component: User }, { path: '/newscontent/:aid', component: NewsContent }, ], })
透過router-link進行跳轉的時候,需要模板字串和冒號+to。
<ul> <li v-for="(item, index) in list" :key="index"> <router-link :to="`/newscontent/${index}`"> {{item}}</router-link> </li> </ul>
透過this.$route.params取得動態路由傳過來的值。
mounted(){ // this.$route.params 获取动态路由的传值 console.log(this.$route.params) }
如果我們想要實作類似與GET傳值,我們可以透過下面的方式
1、將路由配置為普通路由。
const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Home , alias: '/home' }, { path: '/news', component: News }, { path: '/user', component: User }, { path: '/newscontent', component: NewsContent }, ], })
2、router-link透過問號的形式進行跳躍。
<router-link :to="`/newscontent?aid=${index}`"> {{item}}</router-link>
3、透過this.$route.query取得到get傳值。
console.log(this.$route.query);
路由編程式導覽(JS跳轉路由)
只需要透過this.$router.push進行指定即可。
this.$router.push({ path: '/home' })
如果想要實作get傳值,可以透過下列的方式。
this.$router.push({ path: '/home', query: {aid: 14} }) }
動態路由需要使用下面的這種方式。
this.$router.push({ path: '/home/123', // query: {aid: 14} })
路由模式
Hash模式
#Hash模式的典型特點是頁面路由中含有一個井號。
const router = createRouter({ history: createWebHashHistory(), routes: [ ..., ], })
HTML5 history模式
#引入createWebHistory。
router的設定項目中的history屬性設定為createWebHistory()。
import { createRouter, createWebHistory } from 'vue-router' //配置路由 const router = createRouter({ history: createWebHistory(), routes: [ ... ], })
注意:開啟HTML5 History模式之後,發佈到伺服器需要設定偽靜態。
配置偽靜態的方法:
https://router.vuejs.org/zh/guide/essentials/history-mode.html#後端設定範例
#命名路由
一般情況
#定義路由的時候設定name屬性
##
{ path: '/news', component: News,name:"news" }
- 傳入物件進行跳轉
<router-link :to="{name: 'news'}">新闻</router-link>
#透過GET傳值的方式
- ##定義路由的時候配置name屬性
-
{ path: '/newscontent', component: NewsContent, name: "content" },
- 傳入包含query的物件
-
<li v-for="(item, index) in list" :key="index"> <router-link :to="{name: 'content',query: {aid: index}}"> {{item}}</router-link> </li>
- 定義動態路由並指定name屬性
-
#
{ path: '/userinfo/:id', name: "userinfo", component: UserInfo }
<router-link :to="{name: 'userinfo',params: {id: 123}}">跳转到用户详情</router-link>
編程式路由
#和上面的方式很類似。
路由別名<button @click="this.$router.push({name: 'userinfo',params: {id: 666}})">点击跳转</button>路由重定向
{ path: '', redirect: "/home" }, // 路由重定向 { path: '/home', component: Home },
下面的這個實例中,存取people這個路由和存取alias這個路由是一致的。
{ path: '/user', component: User, alias: '/people' }alias也可以是一個陣列。
{ path: '/user', component: User, alias: ['/people','/u']}動態路由的形式。
{ path: '/userinfo/:id', name: "userinfo", component: UserInfo, alias: '/u/:id' }巢狀路由
巢狀路由的應用程式場景一般在導覽列上。
定義巢狀路由
{ path: '/user', component: User, children: [ { path: '', redirect: "/user/userlist" }, { path: 'userlist', component: UserList }, { path: 'useradd', component: UserAdd } ] }
-
<div class="left"> <ul> <li> <router-link to="/user/userlist">用户列表</router-link> </li> <li> <router-link to="/user/useradd">增加用户</router-link> </li> </ul> </div> <div class="right"> <router-view></router-view> </div>
更多程式相關知識,請造訪: 程式設計入門
以上是聊聊Vue3中路由,淺析路由配置方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

whenthevue.jsvirtualdomdetectschange,itupdatesthevirlualdom,diffsit和appliesminimalchangeStothereAldom.thisprocessensuresrocessensureshighhighpperformance byformance byavoidingunnnnnnnnnnneclastory dommaniplastions。

Vue.js的VirtualDOM既是真實DOM的鏡像,又不完全是。 1.創建和更新:Vue.js基於組件定義創建VirtualDOM樹,狀態變化時先更新VirtualDOM。 2.差異和修補:通過diff操作比較新舊VirtualDOM,僅將最小變化應用到真實DOM。 3.效率:VirtualDOM允許批量更新,減少直接DOM操作,優化渲染過程。 VirtualDOM是Vue.js優化UI更新的戰略工具。

Vue.js和React在可擴展性和可維護性上的表現各有優勢。 1)Vue.js易於上手,適合小型項目,CompositionAPI提升了大型項目可維護性。 2)React適用於大型複雜項目,Hooks和虛擬DOM提高了性能和可維護性,但學習曲線較陡峭。

Vue.js和React的未來趨勢和預測分別是:1)Vue.js將在企業級應用中廣泛應用,並在服務端渲染和靜態站點生成方面有突破;2)React將在服務器組件和數據獲取方面創新,並進一步優化並發模式。

Netflix的前端技術棧主要基於React和Redux。 1.React用於構建高性能的單頁面應用,通過組件化開發提升代碼重用性和維護性。 2.Redux用於狀態管理,確保狀態變化可預測和可追踪。 3.工具鏈包括Webpack、Babel、Jest和Enzyme,確保代碼質量和性能。 4.性能優化通過代碼分割、懶加載和服務端渲染實現,提升用戶體驗。

Vue.js是一種漸進式框架,適用於構建交互性強的用戶界面。其核心功能包括響應式系統、組件化開發和路由管理。 1)響應式系統通過Object.defineProperty或Proxy實現數據監聽,自動更新界面。 2)組件化開發允許將界面拆分為可複用的模塊。 3)VueRouter支持單頁面應用,提升用戶體驗。

Vue.js的主要缺點包括:1.生態系統相對較新,第三方庫和工具不如其他框架豐富;2.學習曲線在復雜功能上變得陡峭;3.社區支持與資源不如React和Angular廣泛;4.大型應用中可能遇到性能問題;5.版本升級與兼容性挑戰較大。

Netflix使用React作為其前端框架。 1.React的組件化開發和虛擬DOM機制提高了性能和開發效率。 2.使用Webpack和Babel優化代碼構建和部署。 3.採用代碼分割、服務端渲染和緩存策略進行性能優化。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器