1.跳转页面的三种方法
javascript
<template>
<button @click="twopage">跳转</button>
</template>
<script setup>
import { useRouter } from "vue-router";
const router = useRouter(); // 获取 router 实例
const twopage = () => {
router.push("/two"); // 使用 push 跳转(能够回退)
// router.replace('/two'); // 使用 replace 跳转(不能回退)
// router.go(-1); // 使用 go 跳转(回退到上一页)
};
</script>
<style scoped></style>
2.vue跳转传参方法
1.path跳,query传
javascript
//传
import { useRouter } from "vue-router";
const router = useRouter(); // 获取 router 实例
router.push({ path: "/two", query: { id: id } });
javascript
//接
import { useRoute } from "vue-router";
const route = useRoute();
const userId = route.query.id;
console.log(userId);
2.name跳,query传
javascript
//传
import { useRouter } from "vue-router";
const router = useRouter(); // 获取 router 实例
router.push({ name: "two", query: { id: id } });
javascript
// 接
import { useRoute } from 'vue-router'
const route = useRoute();
console.log(route.query.id);
3. name跳,params传(路由处必须配置动态路由)
javascript
//传
import { useRouter } from "vue-router";
const router = useRouter();
router.push({ name: "two", params: { id } });
javascript
//接
import { useRoute } from "vue-router";
const route = useRoute();
const userId = route.params.id;
console.log(userId);
要记得跳转目标要添加:id