Vue 路由

Vue 中的 路由 用于导航 Vue 应用程序,它发生在客户端(浏览器中),而不需要重新加载整个页面,这会带来更快的用户体验。

路由 是一种导航方式,类似于我们之前使用的方式。

通过 路由,我们可以使用 URL 地址将某人引导到 Vue 应用程序中的特定位置。


使用动态组件导航

为了理解 Vue 中的路由,我们首先来看一个使用动态组件在两个组件之间切换的应用程序。

我们可以使用按钮在组件之间切换:

实例

FoodItems.vue:

  1. <template>
  2. <h1>Food!</h1>
  3. <p>I like most types of food.</p>
  4. </template>

AnimalCollection.vue:

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

App.vue:

  1. <template>
  2. <p>Choose what part of this page you want to see:</p>
  3. <button @click="activeComp = 'animal-collection'">Animals</button>
  4. <button @click="activeComp = 'food-items'">Food</button><br>
  5. <div>
  6. <component :is="activeComp"></component>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. activeComp: ''
  14. }
  15. }
  16. }
  17. </script>
  18. <style scoped>
  19. button {
  20. padding: 5px;
  21. margin: 10px;
  22. }
  23. div {
  24. border: dashed black 1px;
  25. padding: 20px;
  26. margin: 10px;
  27. display: inline-block;
  28. }
  29. </style>

从动态组件到路由

我们使用 Vue 构建 SPAs(单页应用程序),这意味着我们的应用程序只包含一个 *.html 文件。这意味着我们不能将人们引导到其他 *.html 文件,以便在我们的页面上向他们显示不同的内容。

在上面的例子中,我们可以在页面上的不同内容之间导航,但我们不能给其他人一个页面地址,让他们直接进入关于食物的部分,但通过路由,我们可以做到这一点。

通过适当设置路由,如果您打开带有 URL 地址扩展名的 Vue 应用程序,例如 "/food-items",您将直接进入包含食物内容的部分。

安装 Vue 路由库

要在机器上使用 Vue 中的路由,请使用终端在项目文件夹中安装 Vue 路由库:

npm install vue-router@4
更新 main.js

要使用路由,我们必须创建一个路由,我们在 main.js 文件中这样做。

main.js:

  1. import { createApp } from 'vue'
  2. import { createRouter, createWebHistory } from 'vue-router'
  3. import App from './App.vue'
  4. import FoodItems from './components/FoodItems.vue'
  5. import AnimalCollection from './components/AnimalCollection.vue'
  6. const router = createRouter({
  7. history: createWebHistory(),
  8. routes: [
  9. { path: '/animals', component: AnimalCollection },
  10. { path: '/food', component: FoodItems },
  11. ]
  12. });
  13. const app = createApp(App)
  14. app.use(router);
  15. ~~app.component('food-items', FoodItems);~~
  16. ~~app.component('animal-collection', AnimalCollection);~~
  17. app.mount('#app')

增加了第 2 行、第 8-14 行和第 18 行以添加路由器功能。

删除第 19-20 行,因为组件已经通过路由包含在第 11-12 行中。

我们现在已经创建了一个路由,例如,如果在原始 URL 地址的末尾添加了 '/animals',它可以打开 'AnimalCollection' 组件,但直到下一节添加 <router-view> 组件时,它才起作用。路由还可以跟踪网络历史记录,以便您可以在历史记录中来回查看,箭头通常位于网络浏览器中 URL 旁边的左上角。

使用 <router-view> 组件

要使用新路由更改页面上的内容,我们需要删除上一个实例中的动态组件,并使用 <router-view> 组件。

App.vue:

  1. <template>
  2. <p>Choose what part of this page you want to see:</p>
  3. <button @click="activeComp = 'animal-collection'">Animals</button>
  4. <button @click="activeComp = 'food-items'">Food</button><br>
  5. <div>
  6. <router-view></router-view>
  7. ~~<component :is="activeComp"></component>~~
  8. </div>
  9. </template>

如果你在电脑上做了上述更改,你可以在浏览器中将 '/food' 添加到项目页面的URL地址,页面应该更新以显示食物内容,如下所示:

<router-link> 组件

我们可以用 <router-link> 组件替换按钮,因为它与路由配合得更好。

我们不再需要 'activeComp' 数据属性,所以我们可以删除它,实际上我们可以删除整个 <script> 标记,因为它是空的。

App.vue:

  1. <template>
  2. <p>Choose what part of this page you want to see:</p>
  3. <router-link to="/animals">Animals</router-link>
  4. <router-link to="/food">Food</router-link><br>
  5. <div>
  6. <router-view></router-view>
  7. </div>
  8. </template>
  9. ~~<script></script>~~
<router-link> 组件的样式

<router-link> 组件被呈现为 <a> 标记。我们可以看到,如果在浏览器中右键单击元素并对其进行检查:

正如您在上面的屏幕截图中看到的,Vue还跟踪哪个组件是活动组件,并向活动的 <router-link> 组件提供 'router-link-active' 类(现在呈现为 <a> 标记)。我们可以使用上面的信息来突出显示哪个 <router-link> 组件是活动组件:

实例

App.vue:

  1. <template>
  2. <p>Choose what part of this page you want to see:</p>
  3. <router-link to="/animals">Animals</router-link>
  4. <router-link to="/food">Food</router-link><br>
  5. <div>
  6. <router-view></router-view>
  7. </div>
  8. </template>
  9. <style scoped>
  10. a {
  11. display: inline-block;
  12. background-color: black;
  13. border: solid 1px black;
  14. color: white;
  15. padding: 5px;
  16. margin: 10px;
  17. }
  18. a:hover,
  19. a.router-link-active {
  20. background-color: rgb(110, 79, 13);
  21. }
  22. div {
  23. border: dashed black 1px;
  24. padding: 20px;
  25. margin: 10px;
  26. display: inline-block;
  27. }
  28. </style>
注意:在上面的例子中,URL 地址不会被更新,但如果你在自己的机器上这样做,URL 地址就会被更新。即使由于路由由 Vue 中的路由在内部负责,URL 地址没有更新,上面的例子也能工作。