04_Vue Router

来源:https://www.cnblogs.com/MingQiu/p/18122381
-Advertisement-
Play Games

官網:Vue Router | Vue.js 的官方路由 (vuejs.org) 安裝命令:npm install vue-router@4 1.添加兩個頁面\vuedemo\src\views\index.vue、\vuedemo\src\views\content.vue 2.添加\vuedem ...


官網:Vue Router | Vue.js 的官方路由 (vuejs.org)

安裝命令:npm install vue-router@4

1.添加兩個頁面\vuedemo\src\views\index.vue、\vuedemo\src\views\content.vue

2.添加\vuedemo\src\router\index.js文件用來定義路由規則

import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"

//定義路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("../views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("../views/content.vue")
    },
]

const router = createRouter({
    //使用url的#符號之後的部分模擬url路徑的變化,因為不會觸發頁面刷新,所以不需要服務端支持
    //history: createWebHashHistory(),  //哈希模式
    history: createWebHistory(),
    routes }) 

export default router

 

main.js 修改

import { createApp } from 'vue'

//導入Pinia的createPinia方法,用於創建Pinia實例(狀態管理庫)
import { createPinia } from 'pinia'
//從 pinia-plugin-persistedstate 模塊中導入 piniaPluginPersistedstate
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import App from './App.vue'

import router from './router'

const pinia=createPinia();
//將插件添加到 pinia 實例上
pinia.use(piniaPluginPersistedstate)

const app=createApp(App);
app.use(pinia);
app.use(router);
app.mount('#app');

 

app.vue

<script setup>

</script>

<template>
<router-view/>
</template>

<style  scoped>

</style>

 

配置路徑別名@

修改路徑別名文件:\vuedemo\vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path' //導入 node.js path

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: { //配置路徑別名
      '@': path.resolve(__dirname, 'src')
    }
  }
})

 

 修改index.js路徑

//定義路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
]

這裡就是把..修改成@

 

路徑提示設置

添加\vuedemo\jsconfig.json文件

{
    "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@/*": ["src/*"] // 配置 @ 符號指向 src 目錄及其子目錄
      }
    }
  }

 

路徑傳遞參數

//定義路由
const routes = [
    {
        path: "/", // http://localhost:5173
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
    
    {
        path: "/user/:id", 
        component: () => import("@/views/user.vue")
    },
]

訪問路徑:

http://localhost:5173/content?name=張三&age=23

http://localhost:5173/user/5

<template>
<h3>Content頁面.....</h3>
<br>
Name: {{ $route.query.name }} <br>
Age: {{ $route.query.age }}
</template>
<template>
Id: {{ $route.params.id }} <br>
</template>

 

 

index.vue 轉到content.vue

index.vue

<script setup>
 import { useRouter } from 'vue-router';
        const router = useRouter()
        const goTo = ()=> {
            //router.push("/content?name=張三&age=23")
            router.push({ path: '/content', query: { name: '李四', age: 26 } })
        }
</script>

<template>
<h3>Index頁面......</h3>
<br>
<!-- 編程式導航 -->
<button @click="goTo()">編程式導航</button>
</template>

<style  scoped>

</style>

content.vue

<script setup>

</script>

<template>
<h3>Content頁面.....</h3>
<br>
Name: {{ $route.query.name }} <br>
Age: {{ $route.query.age }}
</template>

<style  scoped>

</style>

 

嵌套路由結合共用組件

添加頁面:

\vuedemo\src\views\vip.vue

\vuedemo\src\views\vip\default.vue

\vuedemo\src\views\vip\info.vue

\vuedemo\src\views\vip\order.vue

\vuedemo\src\views\svip.vue

修改index.js

import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"

//定義路由
const routes = [
    {
        path: "/", // http://localhost:5173
        alias:["/home","/index"],
        component: () => import("@/views/index.vue")
    },
    {
        path: "/content", // http://localhost:5173/content
        component: () => import("@/views/content.vue")
    },
    
    {
        path: "/user/:id", 
        component: () => import("@/views/user.vue")
    },
    {
        path: "/vip", 
        component: () => import("@/views/vip.vue"),
        children: [ // 子路由
            {
                path: '', // 預設頁 http://localhost:5173/vip
                component: import("@/views/vip/default.vue")
            },
            {
                path: 'order', // 會員訂單 http://localhost:5173/vip/order
                component: import("@/views/vip/order.vue")
            },
            {
                path: 'info', // 會員資料 http://localhost:5173/vip/info
                component: import("@/views/vip/info.vue")
            }
        ]
    },
    {
        path: "/svip", // http://localhost:5173/svip
        redirect: "/vip" // 重定向
        //redirect: { name: 'history', params: { id: '100', name: 'David' } }
    },
]

const router = createRouter({
    //使用url的#符號之後的部分模擬url路徑的變化,因為不會觸發頁面刷新,所以不需要服務端支持
    //history: createWebHashHistory(), 
    history: createWebHistory(),
    routes
})

export default router

訪問:http://localhost:5173/vip/、http://localhost:5173/vip/info、http://localhost:5173/svip/ (重定向)

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文介紹基於Python語言,讀取Excel表格文件數據,並基於其中某一列數據的值,將這一數據處於指定範圍的那一行加以複製,並將所得結果保存為新的Excel表格文件的方法~ ...
  • 本文提供了一份全面的Kubernetes(K8S)命令指南,旨在幫助用戶掌握和運用K8S的各種命令。 關註【TechLeadCloud】,分享互聯網架構、雲服務技術的全維度知識。作者擁有10+年互聯網服務架構、AI產品研發經驗、團隊管理經驗,同濟本復旦碩,復旦機器人智能實驗室成員,阿裡雲認證的資深架 ...
  • `synchronized`作為Java併發編程的基礎構建塊,其簡潔易用的語法形式背後蘊含著複雜的底層實現原理和技術細節。深入理解`synchronized`的運行機制,不僅有助於我們更好地利用這一特性編寫出高效且安全的併發程式。 ...
  • 用C語言並利用遞歸思想實現設計一個程式,完成斐波那契數列的函數設計,利用遞歸實現! /******************************************************************* * * file name: * author : RISE_AND_GRIN ...
  • 本文結合源碼討論std::shared_ptr和std::weak_ptr的部分底層實現,然後討論引用計數,弱引用計數的創建和增減。 ...
  • SpringApplication類提供了一種從main()方法啟動Spring應用的便捷方式。在很多情況下, 你只需委托給 SpringApplication.run這個靜態方法 : @SpringBootApplication public class SpringbootLearningApp ...
  • C++ Break 和 Continue break 語句還可以用來跳出迴圈。 在以下示例中,當 i 等於 4 時跳出迴圈: for (int i = 0; i < 10; i++) { if (i == 4) { break; } cout << i << "\n"; } C++ Continue ...
  • C++ 20 的 std::format 是一個很神奇、很實用的工具,最神奇的地方在於它能在編譯期檢查字元串的格式是否正確,而且不需要什麼特殊的使用方法,只需要像使用普通函數那樣傳參即可。 #include <format> int a = 1; std::string s1 = std::form ...
一周排行
    -Advertisement-
    Play Games
  • 基於.NET Framework 4.8 開發的深度學習模型部署測試平臺,提供了YOLO框架的主流系列模型,包括YOLOv8~v9,以及其系列下的Det、Seg、Pose、Obb、Cls等應用場景,同時支持圖像與視頻檢測。模型部署引擎使用的是OpenVINO™、TensorRT、ONNX runti... ...
  • 十年沉澱,重啟開發之路 十年前,我沉浸在開發的海洋中,每日與代碼為伍,與演算法共舞。那時的我,滿懷激情,對技術的追求近乎狂熱。然而,隨著歲月的流逝,生活的忙碌逐漸占據了我的大部分時間,讓我無暇顧及技術的沉澱與積累。 十年間,我經歷了職業生涯的起伏和變遷。從初出茅廬的菜鳥到逐漸嶄露頭角的開發者,我見證了 ...
  • C# 是一種簡單、現代、面向對象和類型安全的編程語言。.NET 是由 Microsoft 創建的開發平臺,平臺包含了語言規範、工具、運行,支持開發各種應用,如Web、移動、桌面等。.NET框架有多個實現,如.NET Framework、.NET Core(及後續的.NET 5+版本),以及社區版本M... ...
  • 前言 本文介紹瞭如何使用三菱提供的MX Component插件實現對三菱PLC軟元件數據的讀寫,記錄了使用電腦模擬,模擬PLC,直至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1. PLC開發編程環境GX Works2,GX Works2下載鏈接 https:// ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • 1、jQuery介紹 jQuery是什麼 jQuery是一個快速、簡潔的JavaScript框架,是繼Prototype之後又一個優秀的JavaScript代碼庫(或JavaScript框架)。jQuery設計的宗旨是“write Less,Do More”,即倡導寫更少的代碼,做更多的事情。它封裝 ...
  • 前言 之前的文章把js引擎(aardio封裝庫) 微軟開源的js引擎(ChakraCore))寫好了,這篇文章整點js代碼來測一下bug。測試網站:https://fanyi.youdao.com/index.html#/ 逆向思路 逆向思路可以看有道翻譯js逆向(MD5加密,AES加密)附完整源碼 ...
  • 引言 現代的操作系統(Windows,Linux,Mac OS)等都可以同時打開多個軟體(任務),這些軟體在我們的感知上是同時運行的,例如我們可以一邊瀏覽網頁,一邊聽音樂。而CPU執行代碼同一時間只能執行一條,但即使我們的電腦是單核CPU也可以同時運行多個任務,如下圖所示,這是因為我們的 CPU 的 ...
  • 掌握使用Python進行文本英文統計的基本方法,並瞭解如何進一步優化和擴展這些方法,以應對更複雜的文本分析任務。 ...
  • 背景 Redis多數據源常見的場景: 分區數據處理:當數據量增長時,單個Redis實例可能無法處理所有的數據。通過使用多個Redis數據源,可以將數據分區存儲在不同的實例中,使得數據處理更加高效。 多租戶應用程式:對於多租戶應用程式,每個租戶可以擁有自己的Redis數據源,以確保數據隔離和安全性。 ...