👨‍💻 技术专家 资深开发者 最后更新于 2026-05-30

Vue3VueRouter路由

🎯 学习目标

通过本文,你将深入理解Vue3VueRouter路由的核心原理和实践应用,掌握以下关键技能:

  • 路由配置的基本概念和工作机制
  • 动态路由的进阶技巧和最佳实践
  • 导航守卫的实际应用和性能优化 预计学习时间:12小时

💡 核心概念详解

Vue3VueRouter路由是Vue开发中的关键知识点,掌握这些技能可以构建高质量的Vue应用。

基础用法

<script setup>
import { ref, computed } from 'vue';

const count = ref(0);
const doubled = computed(() => count.value * 2);

function increment() {
  count.value++;
}
</script>

<template>
  <div>
    <p>Count: {{ count }} (doubled: {{ doubled }})</p>
    <button @click="increment">Increment</button>
  </div>
</template>

进阶用法

<script setup>
import { ref, reactive, watch, onMounted, onUnmounted } from 'vue';

const searchQuery = ref('');
const searchResults = ref([]);
const isLoading = ref(false);
let debounceTimer = null;

watch(searchQuery, (newVal) => {
  if (debounceTimer) clearTimeout(debounceTimer);
  debounceTimer = setTimeout(() => {
    performSearch(newVal);
  }, 300);
});

async function performSearch(query) {
  if (!query.trim()) {
    searchResults.value = [];
    return;
  }

  isLoading.value = true;
  try {
    const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`);
    searchResults.value = await response.json();
  } catch (error) {
    console.error('Search failed:', error);
  } finally {
    isLoading.value = false;
  }
}
</script>

<template>
  <div>
    <input 
      v-model="searchQuery" 
      placeholder="Search..."
      :disabled="isLoading"
    />
    <div v-if="isLoading">Loading...</div>
    <ul v-else>
      <li v-for="result in searchResults" :key="result.id">
        {{ result.title }}
      </li>
    </ul>
  </div>
</template>

实战场景

<script setup>
import { ref, provide, inject } from 'vue';

const ThemeSymbol = Symbol('theme');

function ThemeProvider({ children }) {
  const theme = ref('light');

  function toggleTheme() {
    theme.value = theme.value === 'light' ? 'dark' : 'light';
  }

  provide(ThemeSymbol, { theme, toggleTheme });
  return children;
}

function ThemeButton() {
  const { theme, toggleTheme } = inject(ThemeSymbol);
  return (
    <button onClick="toggleTheme">
      Current theme: {theme}
    </button>
  );
}
</script>

<template>
  <ThemeProvider>
    <ThemeButton />
  </ThemeProvider>
</template>

📖 深入理解路由配置

路由配置是Vue3的核心响应式系统。

核心原理

Vue3使用Proxy代理实现响应式,当数据变化时自动更新视图。

关键特性

  1. ref: 创建响应式基本类型
  2. reactive: 创建响应式对象
  3. computed: 计算属性
  4. watch: 侦听器

常见误区

  • 直接赋值: 直接替换reactive对象会失去响应性
  • 解构问题: 解构reactive对象会失去响应性
  • 数组索引: 通过索引修改数组需要特殊处理

📖 掌握动态路由

动态路由是Vue3的组合式API。

优势对比

方案 优点 缺点 适用场景
Options API 易于理解 代码分散 简单组件
Composition API 逻辑聚合 学习曲线 复杂组件

最佳实践

  1. 逻辑复用: 将可复用逻辑提取为composables
  2. 代码组织: 按功能组织代码
  3. 类型支持: 配合TypeScript使用

📖 实践导航守卫

导航守卫是Vue3状态管理的重要内容。

Pinia使用示例

import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    doubled: (state) => state.count * 2
  },
  actions: {
    increment() {
      this.count++;
    }
  }
});

状态持久化

使用pinia-plugin-persistedstate实现状态持久化。

⚖️ 方案对比

方案 优点 缺点 适用场景
Pinia 轻量简洁 生态较小 Vue3项目
Vuex 功能完整 复杂 Vue2项目

📚 推荐学习资源

🚀 实践建议

Vue应用部署推荐使用VercelNetlify

🚀

🎯 推荐工具

想提升你的开发效率?点击下方链接获取专属优惠,开启高效开发之旅!

开始你的项目,DigitalOcean新用户$200额度 →
☁️

🔥 云服务推荐

提升开发效率?GitHub Copilot AI编程助手让你的编码速度翻倍!

免费试用 →