Vue跳转页面传递参数

Vue跳转页面传递参数

[!NOTE]

需求:从搜索页跳到详情页,传递搜索参数到详情页,详情页调用API说去数据,渲染到页面。

路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "layout",
component: () => import("../LayoutView.vue"),
redirect: "/recommend",
children: [
{
path: "/recommend",
name: "recommend",
component: () => import("../views/RecommendView.vue"),
},
{
path: "/detail",
name: "detail",
component: () => import("../views/DetailView.vue"),
},
],
},
],
});

export default router;

页面一

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
import {ref} from "vue";

const search = ref("");
import {useRouter, useRoute} from 'vue-router'
const router = useRouter()
function handleSearch() {
const data = search.value;
router.push({
path: "/detail",
query: {data}
})
}

页面二

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
import {onMounted} from "vue";
import {useRouter, useRoute} from 'vue-router'
import api from "@/api";

const route = useRoute()
const search = route.query.data;
onMounted(() => {
const searchVal = JSON.stringify(search);
api.home.search(searchVal).then((rs: any) => {
console.log(rs.data.result.songs);
})
})
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 何福海
  • 访问人数: | 浏览次数:

请我喝杯奶茶吧~

支付宝
微信