選單
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首頁 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue 為什麼、如何以及設定 Vue 第一個 SFC 頁面 Vue 元件 Vue Props Vue v-for 元件 Vue $emit() Vue 透傳屬性 Vue 作用域樣式 Vue 區域性元件 Vue 插槽 Vue v-slot Vue 作用域插槽 Vue 動態元件 Vue Teleport Vue HTTP 請求 Vue 模板引用 Vue 生命週期鉤子 Vue Provide/Inject Vue 路由 Vue 表單輸入 Vue 動畫 Vue 動畫與 v-for Vue 構建 Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue 路由

Vue 中的路由用於導航 Vue 應用程式,它在客戶端(瀏覽器中)發生,無需完全重新載入頁面,從而提供更快的使用者體驗。

路由是一種導航方式,類似於我們之前使用動態元件的方式。

透過路由,我們可以使用 URL 地址將使用者引導到 Vue 應用程式中的特定位置。

使用動態元件導航

要理解 Vue 中的路由,我們首先來看一個使用動態元件在兩個元件之間切換的應用程式。

我們可以使用按鈕在元件之間切換

示例

FoodItems.vue:

<template>
    <h1>Food!</h1>
    <p>I like most types of food.</p>
</template>

AnimalCollection.vue:

<template>
    <h1>Animals!</h1>
    <p>I want to learn about at least one new animal every year.</p>
</template>

App.vue:

<template>
  <p>Choose what part of this page you want to see:</p>
  <button @click="activeComp = 'animal-collection'">Animals</button>
  <button @click="activeComp = 'food-items'">Food</button><br>
  <div>
    <component :is="activeComp"></component>
  </div>
</template>

<script>
export default {
  data() {
      return {
        activeComp: ''
      }
    }
}
</script>

<style scoped>
  button {
    padding: 5px;
    margin: 10px;
  }
  div {
    border: dashed black 1px;
    padding: 20px;
    margin: 10px;
    display: inline-block;
  }
</style>
執行示例 »

從動態元件到路由

我們使用 Vue 構建 SPA(單頁應用程式),這意味著我們的應用程式只包含一個 *.html 檔案。這意味著我們不能將使用者引導到其他 *.html 檔案來向他們展示頁面上的不同內容。

在上面的示例中,我們可以在頁面上的不同內容之間導航,但我們無法向其他人提供頁面的地址,以便他們直接訪問有關食物的部分,但透過路由我們可以做到這一點。

如果適當設定了路由,當您在 URL 地址後新增副檔名(例如“/food-items”)開啟 Vue 應用程式時,您將直接進入包含食物內容的部分。


安裝 Vue 路由庫

要在您的機器上使用 Vue 路由,請使用終端在您的專案資料夾中安裝 Vue 路由庫

npm install vue-router@4

更新 main.js

要使用路由,我們必須建立一個路由器,我們在 main.js 檔案中完成此操作。

main.js:

import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'

import App from './App.vue'
import FoodItems from './components/FoodItems.vue'
import AnimalCollection from './components/AnimalCollection.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        { path: '/animals', component: AnimalCollection },
        { path: '/food', component: FoodItems },
    ]
});

const app = createApp(App)

app.use(router);
app.component('food-items', FoodItems);
app.component('animal-collection', AnimalCollection);

app.mount('#app')

第 2、8-14 和 18 行已新增,以新增路由器功能。

第 19-20 行已刪除,因為元件已透過第 11-12 行的路由器包含在內。

我們現在已經建立了一個路由器,例如,如果將“/animals”新增到原始 URL 地址的末尾,它將開啟“AnimalCollection”元件,但直到下一節我們新增 <router-view> 元件時,它才能工作。路由器還會跟蹤網頁歷史記錄,以便您可以使用通常位於網頁瀏覽器左上角 URL 旁邊的箭頭在歷史記錄中前後移動。


使用 <router-view> 元件

要使用新路由器更改頁面上的內容,我們需要刪除上一個示例中的動態元件,並改用 <router-view> 元件。

App.vue:

<template>
  <p>Choose what part of this page you want to see:</p>
  <button @click="activeComp = 'animal-collection'">Animals</button>
  <button @click="activeComp = 'food-items'">Food</button><br>
  <div>
    <router-view></router-view>
    <component :is="activeComp"></component>
  </div>
</template>

如果您已在計算機上完成上述更改,則可以在瀏覽器中專案的 URL 地址中新增“/food”,頁面應更新以顯示食物內容,如下所示


使用 <router-link> 元件

我們可以用 <router-link> 元件替換按鈕,因為它與路由器配合得更好。

我們不再需要“activeComp”資料屬性,因此可以刪除它,實際上我們甚至可以刪除整個 <script> 標籤,因為它是空的。

App.vue:

<template>
  <p>Choose what part of this page you want to see:</p>
  <router-link to="/animals">Animals</router-link>
  <router-link to="/food">Food</router-link><br>
  <div>
    <router-view></router-view>
  </div>
</template>

<script></script>

為 <router-link> 元件設定樣式

<router-link> 元件渲染為 <a> 標籤。如果我們在瀏覽器中右鍵單擊元素並檢查它,我們可以看到這一點

正如您在上面的截圖中看到的,Vue 還會跟蹤哪個元件是活動元件,併為活動的 <router-link> 元件(現在渲染為 <a> 標籤)提供“router-link-active”類。

我們可以使用上面的資訊來設定樣式,以突出顯示哪個 <router-link> 元件是活動的

示例

App.vue:

<template>
  <p>Choose what part of this page you want to see:</p>
  <router-link to="/animals">Animals</router-link>
  <router-link to="/food">Food</router-link><br>
  <div>
    <router-view></router-view>
  </div>
</template>

<style scoped>
  a {
    display: inline-block;
    background-color: black;
    border: solid 1px black;
    color: white;
    padding: 5px;
    margin: 10px;
  }
  a:hover,
  a.router-link-active {
    background-color: rgb(110, 79, 13);
  }
  div {
    border: dashed black 1px;
    padding: 20px;
    margin: 10px;
    display: inline-block;
  }
</style>
執行示例 »

注意:在上面的示例中,URL 地址未更新,但如果您在自己的機器上執行此操作,URL 地址將會更新。即使 URL 地址未更新,上面的示例也有效,因為路由由 Vue 中的路由器在內部處理。


Vue 練習

透過練習來測試自己

練習

Vue 中的路由在“main.js”檔案中設定。

填寫以下空白,以便成功建立路由。

import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'

import App from './App.vue'
import FishTypes from './components/FishTypes.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        { : '/fish', : FishTypes }
    ]
});

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

開始練習



×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援