普通视图

发现新文章,点击刷新页面。
今天 — 2026年4月1日首页

最新版vue3+TypeScript开发入门到实战教程之组件通信之一

作者 angerdream
2026年3月31日 22:38

概述

Vue 组件通信是面试和实际开发中的核心知识点,组件通信包括父组件与子组件甚至孙组件的通信,子组件与子组件之间的通信。组件通信详细掌握以下9中方法:

  • props
  • 自定义事件
  • mitt
  • v-model
  • $attrs
  • refsrefs、parent
  • provide、inject
  • pinia
  • slot

props

概述

props是组件通信中最常用的一种方式,常用于父与子之间数据传递

  • 父传子,通过数据传递
  • 子传父,通过函数传递数据

事例

  • 创建父组件App,数据为name、price
  • 创建子组件Fish,数据为num
  • 父传子数据name、price,父传子函数,子调用函数传数量num
  • 子接收数据、函数 App组件代码
<template>
  <div class="app">
    <h2>父组件接收到鱼的数量:{{ num }}</h2>
    <Fish :fishname="name" :price="price" :getNum="getNum"/>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Fish from './view/Fish.vue';
let name = ref('鲫鱼');
let price = ref(100);
let num = ref(0);
function getNum(n: number) {
  num.value= n;
}

Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ fishname }}</h2>
    <h2>价格:{{ price }}</h2>
    <button @click="getNum(num++)">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
let num = ref(999);
defineProps(['fishname','price','getNum'])
</script>

运行效果 在这里插入图片描述

自定义事件

父组件给子组件绑定自定义事件,子组件触发事件,回调父组件函数,传递数据,类似@click。自定义事件只能由子组件传递给父组件

  • 父组件传递自定义函数
  • 子组件触发自定义函数,传递参数 App组件代码
<template>
  <div class="app">
    <h2>父组件接收到鱼:{{ name }}</h2>
    <h2>父组件接收到鱼的价格:{{ price }}</h2>
    <Fish @get-fish="getFish"/>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Fish from './view/Fish.vue';
let name = ref('');
let price = ref(0);
function getFish(s:string,p:number) {
  name.value = s;
  price.value = p;
}
</script>

Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}</h2>
    <button @click="emit('get-fish',name,price)">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
let name = ref('鲫鱼');
let price = ref(999);
const emit = defineEmits(['get-fish']);
</script>

运行效果: 在这里插入图片描述

mitt

概述

mitt是一个第三方库,它可以实现任意组件通信。组件通过订阅接收消息,通过发布传递数据。

  • emitter.on(event, handler)监听事件
  • emitter.emit(event, data)触发事件并传递数据
  • emitter.off(event, handler)移除事件监听
  • emitter.all.clear()清空所有事件

事例

以子组件传递数据,父组件接收数据为例。同理若父组件给数据,子组件接收数据是一样的。

  • 安装mitt,npm install mitt
  • 创建utils目录,创建emitter.ts
  • main.ts引入emitter.ts
  • 创建App接收Fish传递数据,
  • 创建Fish,发送数据

emitter.ts代码

import mitt from 'mitt'
const emitter = mitt();
export default emitter;

main.ts代码

import { createApp } from 'vue'
import App from './App.vue'
import emitter from './utils/emitter'
console.log(emitter)
const app = createApp(App)
app.mount('#app')

App组件代码

<template>
  <div class="app">
    <h2>父组件接收到鱼:{{ name }}</h2>
    <h2>父组件接收到鱼的价格:{{ price }}</h2>
    <Fish/>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Fish from './view/Fish.vue';
import emitter from '@/utils/emitter'
let name = ref('');
let price = ref(0);
emitter.on('get-fish', (value:any) => {
  console.log(value)
  name.value = value.name;
  price.value=value.price
})
</script>

Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ fish.name }}</h2>
    <h2>价格:{{ fish.price }}</h2>
    <button @click="emitter.emit('get-fish',fish)">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
import emitter from '@/utils/emitter'
emitter.emit('test',121)
let fish = reactive({
  name: '鲫鱼',
  price:99
});
</script>

v-model

概述

v-model是双向数据绑定,它的底层是通过props参数modelValue与自定义函数update:modelValue实现的。在子组件接收props参数与自定义函数,就可以实现双向数据绑定。

事例

App代码

<template>
  <div class="app">
    <h2>父组件接收到鱼:{{ name }}</h2>
    <Fish v-model="name"/>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Fish from './view/Fish.vue';
let name = ref('鲫鱼');
</script>

Fish代码

<template>
  <div>
    <h2>鱼类:{{ modelValue }}</h2>
    <button @click="changefish">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
let props = defineProps(['modelValue']);
const emit = defineEmits(['update:modelValue']);
function changefish() {
  emit('update:modelValue','草鱼')

}
</script>

运行查看结果 在这里插入图片描述 注意代码,v-model默认传递props参数是modelValue、自定义函数式update:modelValue,注意有冒号也是个函数。以下修改v-model props名称、自定义函数名称。

事例2修改v-model props名称与自定义函数名称

App源码

<template>
  <div class="app">
    <h2>父组件接收到鱼:{{ name }}</h2>
    <h2>父组件接收到鱼:{{ price }}</h2>
    <Fish v-model="name" v-model:price="price"/>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Fish from './view/Fish.vue';
let name = ref('鲫鱼');
let price = ref(666);
</script>

Fish源码

<template>
  <div>
    <h2>鱼类:{{ modelValue }}</h2>
    <h2>鱼类:{{ price }}</h2>
    <button @click="changefish">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
let props = defineProps(['modelValue','price']);
const emit = defineEmits(['update:modelValue','update:price']);
function changefish() {
  emit('update:modelValue','草鱼')
  emit('update:price','999')

}
</script>

运行效果如下: 在这里插入图片描述

昨天以前首页

最新版vue3+TypeScript开发入门到实战教程之Pinia详解

作者 angerdream
2026年3月30日 16:34

概述

Pinia 是 Vue.js 的官方状态管理库,可以把它看作是 Vuex 的升级版。它提供了更简洁的 API 和更好的 TypeScript 支持,已经成为 Vue 生态中推荐的状态管理方案。Pinia基本三要素:

  • store ,数据,用户自定义数据存储在store
  • getters,获取数据或进行加工后的数据,类似计算属性computed
  • actions,修改数据的方法

Pinia存储读取数据的基本方法

  • 安装Pinia,npm install pinia
  • 在main.ts引入Pinia,创建引用实例
  • 创建Fish组件,数据name,price,site
  • 创建store文件夹,创建useFishStore,存储Fish组件数据 文件结构目录 在这里插入图片描述 main.ts代码:
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia'
const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.mount('#app')

Fis组件代码

<template>
  <div>
    <h2>鱼类:{{ store.name }}</h2>
    <h2>价格:{{ store.price }}</h2>
    <h2>位置:{{ store.site }}</h2>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
const store = useFishStore();
</script>

useFishStore.ts代码

import { defineStore } from 'pinia'
export const useFishStore = defineStore('fish', {
  state: () => ({
    name: '鲫鱼',
    price: 10,
    site:'河里'
  })
})

运行效果 在这里插入图片描述

Pinia修改数据的三种方法

  • 直接修改
  • 通过$patch方法修改
  • 通过actions修改

直接修改数据

Fish组件

<template>
  <div>
    <h2>鱼类:{{ store.name }}</h2>
    <h2>价格:{{ store.price }}</h2>
    <h2>位置:{{ store.site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
const store = useFishStore();
function changeFish() {
  store.name += '~';
  store.price += 10;
  store.site+='!'

}
</script>

修改效果如下: 在这里插入图片描述

通过$patch方法修改

Fish组件源码

<template>
  <div>
    <h2>鱼类:{{ store.name }}</h2>
    <h2>价格:{{ store.price }}</h2>
    <h2>位置:{{ store.site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
const store = useFishStore();
function changeFish() {
   store.$patch({
    name: '带鱼',
    price: 300,
    site:'海里'
  });
}
</script>

修改效果如图: 在这里插入图片描述

通过actions修改

useFishStore增加actions,添加方法changeFish

import { defineStore } from 'pinia'
export const useFishStore = defineStore('fish', {
  state: () => ({
    name: '鲫鱼',
    price: 10,
    site:'河里'
  }),
  actions: {
    changeFish(fish: any) {
      this.name = fish.name;
      this.price = fish.price;
      this.site = fish.site
    }
  }
})

Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ store.name }}</h2>
    <h2>价格:{{ store.price }}</h2>
    <h2>位置:{{ store.site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
const store = useFishStore();
function changeFish() {
  store.changeFish({
    name: '带鱼',
    price: 300,
    site: '海里'
  });
}
</script>

运行效果如下: 在这里插入图片描述

Pinia函数storeToRefs应用

在Fish引用useFishStore,从useFishStore()直接解析数据,会丢失响应式,需要使用toRefs转换,但toRefs会将所有成员变成响应式对象。storeToRefs只会将数据转换成响应式对象。 Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}</h2>
    <h2>位置:{{ site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
import { toRefs } from 'vue';
import { storeToRefs } from 'pinia'
console.log(toRefs(useFishStore()));
console.log(storeToRefs(useFishStore()));
let { name, price, site } = storeToRefs(useFishStore());

function changeFish() {
  name.value += '~';
  price.value += 2;
  site.value += '!';

}
</script>

运行效果如图,注意控制台打印的日志: 在这里插入图片描述

Getters用法

类似组件的 computed,对state 数据进行派生计算。state数据发生改变,调用getters函数。 useFishStore.ts代码:

import { defineStore } from 'pinia'
export const useFishStore = defineStore('fish', {
  state: () => ({
    name: '鲫鱼',
    price: 10,
    site:'河里'
  }),
  actions: {
    changeFish(fish: any) {
      this.name = fish.name;
      this.price = fish.price;
      this.site = fish.site
    }
  },
  getters: {
    changeprice():number {
      return this.price * 20;
    },
    changesite():string {
      return this.name+'在'+this.site+'游泳'
    }
  }
})

注意changeprice():number,ts语法检查,函数返回类型为number。 Fish组件代码

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}新价格:{{ changeprice }}</h2>
    <h2>位置:{{ site }}新位置:{{ changesite }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
import { toRefs } from 'vue';
import { storeToRefs } from 'pinia'
console.log(toRefs(useFishStore()));
console.log(storeToRefs(useFishStore()));
let { name, price, site,changeprice,changesite } = storeToRefs(useFishStore());

function changeFish() {
  name.value += '~';
  price.value += 2;
  site.value += '!';

}
</script>

运行效果如图: 在这里插入图片描述

$subscribe用法

subscribe订阅信息,当数据发生变化,回调subscribe订阅信息,当数据发生变化,回调subscribe函数设定的回调函数,该函数有两个参数:一是事件信息,一是修改后的数据数据。 $subscribe用于两组件的数据通信,Fish组件数据发生变化时,通知Cat组件。

import { defineStore } from 'pinia'
export const useFishStore = defineStore('fish', {
  state: () => ({
    name: '鲫鱼',
    price: 10,
    site:'河里'
  }),
  actions: {
    changeFish(fish: any) {
      this.name = fish.name;
      this.price = fish.price;
      this.site = fish.site
    }
  }
})

Fish组件:

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}</h2>
    <h2>位置:{{ site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
import { storeToRefs } from 'pinia'
let store = useFishStore()
let { name, price, site } = storeToRefs(store);
function changeFish() {
  name.value += '~';
  price.value += 2;
  site.value += '!';

}
</script>

Cat组件

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}</h2>
    <h2>位置:{{ site }}</h2>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
import { ref } from 'vue';
let name = ref('');
let price = ref(0);
let site=ref('')
let store = useFishStore();
store.$subscribe((mutate, state) => {
  console.log(mutate);
  console.log(state);
  name.value = state.name;
  price.value = state.price;
  site.value = state.site;
});

</script>

效果如图: 在这里插入图片描述 注意控制台打印的数据

Pinia组合式写法

组合式是vue3中新语法,有以下优势,

  • 轻松提取和组合业务逻辑
  • 使用所有 Vue 组合式 API(ref、computed、watch、生命周期等)
  • 逻辑可以聚合在一起,而不是分散在不同配置项中
import { defineStore } from 'pinia'
import { computed, ref } from 'vue';
export const useFishStore = defineStore('fish', () => {
  let name = ref('鲫鱼');
  let price = ref(10);
  let site = ref('河里');
  function changeFish(fish: any) {
    console.log(fish)
    name.value = fish.name;
    price.value = fish.price;
    site.value = fish.site;
  }
  let calcPrice = computed(() => {
    return price.value * 2;

  })
  return { name, price,site,changeFish,calcPrice };

})

Fish组件

<template>
  <div>
    <h2>鱼类:{{ name }}</h2>
    <h2>价格:{{ price }}新价格:{{ calcPrice }}</h2>
    <h2>位置:{{ site }}</h2>
    <button @click="changeFish()">修改鱼的数据</button>
  </div>
</template>
<script setup lang="ts">
import { useFishStore } from '@/store/useFishStore'
import { storeToRefs } from 'pinia'
let store = useFishStore()
let { name, price, site ,calcPrice} = storeToRefs(store);
function changeFish() {
  store.changeFish({ name: '带鱼', price: 11, site: '海里' })

}
</script>

运行效果 在这里插入图片描述

最新版vue3+TypeScript开发入门到实战教程之路由详解三

作者 angerdream
2026年3月27日 16:27

上节内容是嵌套路由与路由传参。路由的传参有两方式,一是query,一是params。这节主要内容:

  • 路由的props
  • 路由的replace属性
  • 路由编程式导航
  • 路由的重定向

1、 什么是路由的props

前几节详细讲解组件的props,父组件给子组件传递参数,就是通过props。如下例:父组件将变量fish、price传递给子组件fish

<Fish :name="fish" :price="price"/>

子组件通过defineProps接收参数,可在模版中直接使用

<template>
  <h2>我是子组件</h2>
  <h3>{{ name }}</h3>
  <h3>{{ price }}</h3>
</template>
<script setup lang="ts">
defineProps(['name', 'price']);
</script>

1.1路由的props

路由传参有两种方式,一是通过query,一是通过params,props就是第三种方式。用户通过点击router-link标签时,会跳转到对应的路由,此时组件被创建。是路由创建的组件。以下是路由的props的含义: 路由在创建组件时,可把路由参数通过组件的props传递给组件。 路由传递参数给组件的props,有两种方式:

  • 默认传参params
  • 通过函数传递自定义数据

1.2props默认传参

props默认传参是params。以父组件Fish点击路由,跳转到Fishdetail组件为例。

  • 创建Fish组件传递params参数
  • 创建路由,路由配置props参数为true
  • 创建FishDetail组件,通过defineProps接收props参数 Fish组件代码
<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link
         :to="{name:'fishdetail',params:{id:item.id,name:item.name,price:item.price}}">{{ item.name }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

FishDetail组件代码

<template>
  <div>
    <h3>鱼类:{{ name }}</h3>
    <h3>id:{{ id }}</h3>
    <h3>价格:{{ price }}</h3>
  </div>
</template>
<script setup lang="ts">
defineProps(['name', 'id', 'price']);
</script>

路由代码

import { createRouter, createWebHistory } from 'vue-router'
import Fish from '@/view/Fish.vue'
import Cat from '@/view/Cat.vue'
import Bird from '@/view/Bird.vue'
import FishDetial from '@/view/FishDetial.vue'
const routes = [
  {
    name: 'fish',
    path: '/fish',
    component: Fish,
    children: [
      {
        name: 'fishdetail',
        path: 'detail/:id/:name/:price?',
        component: FishDetial,
        props:true
      }
    ]
  },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }, // 动态路由
]
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
})
export default router

运行查看效果 在这里插入图片描述 对比路由params传参与props默认传参的区别

  • props默认传参,在子组件中不需要通过useRoute()接收路由参数
  • props默认传参,通过defineProps接收数据,可直接在模版中使用
  • props默认传参,在路由中设置props为true即可

1.3路由props通过函数传递自定义数据

自定义数据可以根据需要来定义,以传递query数据为例。 路由配置:

  import { createRouter, createWebHistory } from 'vue-router'
  import Fish from '@/view/Fish.vue'
  import Cat from '@/view/Cat.vue'
  import Bird from '@/view/Bird.vue'
  import FishDetial from '@/view/FishDetial.vue'
  console.log(createRouter)
  const routes = [
    {
      name: 'fish',
      path: '/fish',
      component: Fish,
      children: [
        {
          name: 'fishdetail',
          path: 'detail',
          component: FishDetial,
          props(route: any) {
            return route.query;
          }
        }
      ]
    },
    { path: '/cat', component: Cat },
    { path: '/bird', component: Bird }, // 动态路由
  ]
  const router = createRouter({
    history: createWebHistory(),
    routes: routes,
  })
  export default router

Fish代码

<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link
         :to="{name:'fishdetail',query:{id:item.id,name:item.name,price:item.price}}">{{ item.name }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

FishDetail代码

<template>
  <div>
    <h3>鱼类:{{ name }}</h3>
    <h3>id:{{ id }}</h3>
    <h3>价格:{{ price }}</h3>
  </div>
</template>
<script setup lang="ts">
defineProps(['name', 'id', 'price']);
</script>

注意核心代码分两处,一是路由的配置,一是route-link跳转

 children: [
        {
          name: 'fishdetail',
          path: 'detail',
          component: FishDetial,
          props(route: any) {
            return route.query;
          }
        }
      ]
 <router-link
      :to="{name:'fishdetail',query:{id:item.id,name:item.name,price:item.price}}">
         {{ item.name }}
 </router-link>

2、路由的replace属性

与push相对,当点击路由进入页面时。默认是push模式,push是一个一个页面堆叠在一起,点击浏览器返回键,可返回到上一页面。replace属性则不是,它只有一个页面,当点击路由时,它替换当前页面。如下:如需要给标签加上replace即可

    <router-link replace :to="{name:'fish'}">跳转到鱼</router-link>
<router-link replace to="/cat">跳转到猫</router-link>
    <router-link replace to="/bird">跳转到鸟</router-link>

如图,点击路由跳转时,无法后退 在这里插入图片描述

3、编程式路由导航

编程式路由导航是在开发中使用最常见的一种方式,而前边使用的router-link,实则就是a标签。

<a href="/cat" class="">跳转到鱼</a>
<router-link replace to="/cat">跳转到猫</router-link>

这两种写法等效。编程式导航是使用api跳转路由,如打开页面三秒跳转到cat页面,再如用户登录成功后跳转到个人页面。这些都需要编程式导航。

  • 创建Fish组件、FishDetail
  • 在Fish组件引入useRouter函数,创建路由器router,注意与route区别
  • router使用push或者replace跳转到对应路由
  • push或者replace函数的参数与router-link中的to参数使用方法是一样的 Fish组件
<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <!-- <router-link :to="`/fish/detail/${item.id}/${item.name}`">{{item.name  }}</router-link> -->
         <button @click="goDetail(item)">查看{{ item.name }}</button>
        <!-- <router-link
         :to="{name:'fishdetail',query:{id:item.id,name:item.name,price:item.price}}">{{ item.name }}</router-link> -->
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
let router = useRouter();
function goDetail(fish: any) {
  router.push({
    name: 'fishdetail',
    query:fish
  });
}
</script>

FishDetail组件源码

<template>
  <div>
    <h3>鱼类:{{ name }}</h3>
    <h3>id:{{ id }}</h3>
    <h3>价格:{{ price }}</h3>
  </div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
// let route = useRoute();
defineProps(['name', 'id', 'price']);
</script>

路由源码

  import { createRouter, createWebHistory } from 'vue-router'
  import Fish from '@/view/Fish.vue'
  import Cat from '@/view/Cat.vue'
  import Bird from '@/view/Bird.vue'
  import FishDetial from '@/view/FishDetial.vue'
  console.log(createRouter)
  const routes = [
    {
      name: 'fish',
      path: '/fish',
      component: Fish,
      children: [
        {
          name: 'fishdetail',
          path: 'detail',
          component: FishDetial,
          props(route: any) {
            return route.query;
          }
        }
      ]
    },
    { path: '/cat', component: Cat },
    { path: '/bird', component: Bird }, // 动态路由
  ]
  const router = createRouter({
    history: createWebHistory(),
    routes: routes,
  })
  export default router

运行查看效果: 在这里插入图片描述 注意对比一下router.push的参数与router-link的to参数。两者参数用法是一致的,不管路由如何配置,是使用query还是params传参还是props传参。 如下例:

 <router-link
         :to="{name:'fishdetail',query:{id:item.id,name:item.name,price:item.price}}">
         {{ item.name }}</router-link>
function goDetail(fish: any) {
  router.push({
    name: 'fishdetail',
    query:fish
  });
}

router-link的to用法与 router.push用法一致。

4、路由的重定向

路由的重定向,就是访问a路由,自动跳转到b路由。如打开主页,默认访问http://localhost:5173/,访问的路径是/,能否一打开就跳转到/fish。就用重定向来解决。

 {
    path: '/',
    redirect: '/fish'

  },

路由具体代码:

  import { createRouter, createWebHistory } from 'vue-router'
  import Fish from '@/view/Fish.vue'
  import Cat from '@/view/Cat.vue'
  import Bird from '@/view/Bird.vue'
  import FishDetial from '@/view/FishDetial.vue'
  console.log(createRouter)
const routes = [
  {
    path: '/',
    redirect: '/fish'

  },
  {
    name: 'fish',
    path: '/fish',
    component: Fish,
    children: [
      {
        name: 'fishdetail',
        path: 'detail',
        component: FishDetial,
        props(route: any) {
          return route.query;
        }
      }
    ]
  },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }, // 动态路由
]
  const router = createRouter({
    history: createWebHistory(),
    routes: routes,
  })
  export default router

效果是一打开页面,就重定向到fish页面 在这里插入图片描述

最新版vue3+TypeScript开发入门到实战教程之路由详解二

作者 angerdream
2026年3月26日 15:24

上节讲解到路由的基础用法,路由的内容还有很多,这节开始路由嵌套与传参。

1、路由的嵌套

路由嵌套,简单言之,由主页点击进入一个模块,在模块中又可点击进入子模块。如在主页进入Fish组件,在Fish组件中,进入FishDetail详细内容。路由的四个基本要素

  • 路由管理器,统一管理路由
  • 路由信息,记录组件与路由的对应关系
  • 跳转标签与跳转方法,用于跳转指定路由
  • 路由跳转后,指定组件显示位置

创建一个简单实例实现嵌套路由

  • 创建主页,主页含有标题、导航、路由跳转子页面显示位置
  • 创建三个子页面,Fish、Cat、Bird
  • 创建FishDetail组件
  • 创建路由及路由信息
  • 创建Fish子路由

路由源码

import { createRouter, createWebHistory } from 'vue-router'
import Fish from '@/view/Fish.vue'
import Cat from '@/view/Cat.vue'
import Bird from '@/view/Bird.vue'
import FishDetial from '@/view/FishDetial.vue'
console.log(createRouter)
const routes = [
  {
    name: 'fish',
    path: '/fish',
    component: Fish,
    children: [
      {
        name: 'fishdetail',
        path: 'detail',
        component:FishDetial
      }
    ]
  },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }, // 动态路由
]
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
})
export default router

App组件源码

<template>
  <div class="app">
    <router-link :to="{name:'fish'}">跳转到鱼</router-link>
    <router-link to="/cat">跳转到猫</router-link>
    <router-link to="/bird">跳转到鸟</router-link>
    <div class="content">
    <router-view></router-view>
    </div>
  </div>
</template>
<script setup lang="ts">
</script>

Fish组件源码

<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link :to="{path:'/fish/detail'}">{{ item.name }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

FishDetal组件源码

<template>
  <div>
    <h3>鱼类:鲫鱼</h3>
    <h3>id:01</h3>
    <h3>价格:100</h3>
  </div>
</template>
<script setup lang="ts">
</script>

运行查看效果: 在这里插入图片描述

2、路由的query传参

当我们点击路由进入子页面时,希望把数据传给子页面。路由的传参,有两种方式。

  • query传参,url格式类似/fish/detail?id=01&name=鲫鱼&price=100
  • parmas传参,url格式类似/fish/detail/02/草鱼 query传参通过url地址传递给子组件,子组件通过useRoute函数接收query参数。传参实例详见Fish和FishDetail组件. Fish组件源码
<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link
         :to="{path:'/fish/detail',query:{id:item.id,name:item.name,price:item.price}}">{{ item.name }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

FishDetail组件源码

<template>
  <div>
    <h3>鱼类:{{ route.query.name }}</h3>
    <h3>id:{{ route.query.id }}</h3>
    <h3>价格:{{ route.query.price }}</h3>
  </div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
let route=useRoute();
</script>

运行实例查看效果 在这里插入图片描述 注意在FishDetail,获取到的route,是响应式。若我们从route解析获取query,将失去响应式。

<template>
  <div>
    <h3>鱼类:{{ query.name }}</h3>
    <h3>id:{{ query.id }}</h3>
    <h3>价格:{{ query.price }}</h3>
  </div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
let { query } = useRoute();
</script>

如图: 在这里插入图片描述 注意url地址的变化,若要query具有相应式,可通过toRefs函数,转换为响应式。如下

let { query } = toRefs(useRoute());

3、路由的params传参

params是通过url接收参数,格式类似:fish/detail/02/草鱼/150,可通过useRoute函数获取params参数。与query不同,params必须在路由中设置对应的参数。还是以Fish与FishDetail为例,重新修改代码: 路由代码

import { createRouter, createWebHistory } from 'vue-router'
import Fish from '@/view/Fish.vue'
import Cat from '@/view/Cat.vue'
import Bird from '@/view/Bird.vue'
import FishDetial from '@/view/FishDetial.vue'
console.log(createRouter)
const routes = [
  {
    name: 'fish',
    path: '/fish',
    component: Fish,
    children: [
      {
        name: 'fishdetail',
        path: 'detail/:id/:name/:price?',
        component:FishDetial
      }
    ]
  },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }, // 动态路由
]
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
})
export default router

Fish组件代码

<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link
         :to="{name:'fishdetail',params:{id:item.id,name:item.name,price:item.price}}">{{ item.name }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

FishDetail组件代码

<template>
  <div>
    <h3>鱼类:{{ route.params.name }}</h3>
    <h3>id:{{ route.params.id }}</h3>
    <h3>价格:{{ route.params.price }}</h3>
  </div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
let route = useRoute();
</script>

运行实例查看params 在这里插入图片描述 注意两点:

  • router-link使用跳转路由时,用的是name,而不能用path
  • router-link传递参数不能数组
  • 在路由配置中price后有问号,代表这个参数可不用传递 若不使用name,可以通过下面代码跳转:
<template>
  <div>
    <ul>
      <li v-for="item in fishs" :key="item.id">
        <router-link :to="`/fish/detail/${item.id}/${item.name}`">{{item.name  }}</router-link>
      </li>
    </ul>
    <RouterView/>
  </div>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
let fishs = reactive([
  {id:'01',name:'鲫鱼',price:100},
  {id:'02',name:'草鱼',price:150},
  {id:'03',name:'鲈鱼',price:200},
])
</script>

其实to后面就是字符串拼接。

最新版vue3+TypeScript开发入门到实战教程之路由详解

作者 angerdream
2026年3月25日 16:43

1、概述

网站是有许多单页面组成,页面并非孤立,而是可以相互跳转。以下是官网给的定义: Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。它的核心价值在于:在不刷新页面的情况下,根据 URL 的变化动态渲染不同的组件,实现流畅的页面切换体验。 假设网站有四个页面,主页,a、b、c,网站可以从主页分别跳转到a、b、c是三个页面。也可跳回主页。这些跳转信息,称作路由信息,管理路由信息完成跳转称作路由器。路由四大要素:

  • 路由管理器,统一管理路由
  • 路由信息,记录组件与路由的对应关系
  • 跳转标签与跳转方法,用于跳转指定路由
  • 路由跳转后,指定组件显示位置

2、 基本路由导航实例

  • 创建主页,主页含有标题、导航、路由跳转子页面显示位置
  • 创建三个子页面,Fish、Cat、Bird
  • 创建路由器,挂载路由器
  • 创建路由信息

2.1创建路由器、路由信息、挂载路由器

2.1.1创建路由器、路由信息

const routes = [
  { path: '/fish', component: Fish },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }  // 动态路由
]
const router = createRouter(
  {
    history: createWebHistory(),
    routes: routes
  }
)

路由信息routes,注意routerroutes区别。routes包含path与component。

  • path是路径,浏览器地址,url如:http://localhost:5173/bird,访问bird页面
  • component组件,路径path对应的组件 路由器的创建,包含路由信息与history。history有两种模式:
  • createWebHistory。传统模式,url美观,seo友好
  • createWebHashHistory 。hash模式,url地址含有#,不美观,兼容性好

2.1.2挂载路由器

挂载路由器,要在创建vue实例后,挂载路由。vue实例是在main.ts中创建。

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

2.2路由基本切换效果

在这里插入图片描述

首先打开页面,框内为空。分别点击按钮,跳转到响应页面,内容出现在边框内,注意url地址变化。

2.2.1 目录文件结构

在这里插入图片描述

2.2.2 main.ts源码

import { createApp } from 'vue'
import App from './App.vue'
import router from '@/router/index'
const app = createApp(App)
app.use(router)
app.mount('#app')

2.2.1 router/index.ts源码

import { createRouter,createWebHistory } from "vue-router";
import Fish from "@/view/Fish.vue";
import Cat from "@/view/Cat.vue";
import Bird from "@/view/Bird.vue";
console.log(createRouter);
const routes = [
  { path: '/fish', component: Fish },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }  // 动态路由
]
const router = createRouter(
  {
    history: createWebHistory(),
    routes: routes
  }
)
export default router;

2.2.1 App源码

<template>
  <div class="app">
    <router-link to="/fish">跳转到鱼</router-link>
    <router-link to="/cat">跳转到猫</router-link>
    <router-link to="/bird">跳转到鸟</router-link>
    <div class="content">
    <router-view></router-view>
    </div>
  </div>
</template>
<script setup lang="ts">
</script>

2.2.1 Fish、cat、Bird源码

Fish

<template>
  <div>
    <h1>会游泳的鲫鱼</h1>
  </div>
</template>
<script setup lang="ts">
</script>

Cat

<template>
  <div>
    <h1>爱吃老鼠的猫</h1>
  </div>
</template>
<script setup lang="ts">
</script>

Bird

<template>
  <div>
    <h1>翱翔天空的小鸟</h1>
  </div>
</template>
<script setup lang="ts">
</script>

2.3路由的两个注意点

  • 路由组件,如Fish、Cat等,应存放在pages或者views文件夹内,而非components文件夹内
  • 点击导航按钮,路由的切换,是旧页面组件的销毁,新页面组件创建的过程。

3、路由的工作模式

路由的工作模式有两种,在创建路由时,必须给定模式 -history -hash history是传统模式,优点是URL更加美观,更接近传统网站URL。缺点是后期项目上线,后台服务器需配合处理路径问题,否则报404错误。一般用history较多,如b站。 hash兼容性更好,不需要服务器后台处理路径问题。缺点是url带有#,不美观,且SEO优化方面差,后端项目常用。 以下是hash实例,与history不同之处在创建路由时,用createWebHashHistory 函数指定hash模式: router/index.ts代码

import { createRouter,createWebHashHistory } from "vue-router";
import Fish from "@/view/Fish.vue";
import Cat from "@/view/Cat.vue";
import Bird from "@/view/Bird.vue";
console.log(createRouter);
const routes = [
  { path: '/fish', component: Fish },
  { path: '/cat', component: Cat },
  { path: '/bird', component: Bird }  // 动态路由
]
const router = createRouter(
  {
    history: createWebHashHistory(),
    routes: routes
  }
)
export default router;

运行效果: 在这里插入图片描述 注意路径带有#

4、路由跳转To的三种用法与路由命名

router-link有三种用法,以跳转为例Fish,重新配置Fish组件路由信息,给Fish路由命名为yu。 如下:{ name:'yu',path: '/fish', component: Fish }。 router-link有三种方式可以跳转到Fish组件

    <router-link :to="{name:'yu'}">跳转到鱼</router-link>
    <router-link :to="{path:'fish'}"">跳转到鱼</router-link>
    <router-link :to="/fish">跳转到鱼</router-link>

三种跳转方式各有利弊,常用第二种方式,便于路由传参。

❌
❌