const routes: RouteRecordRaw[] = [
{
path: '/',
name: 'Hello',
component: Hello,
},
{
path: '/next-page/:message',
name: 'NextPage',
component: NextPage,
},
];
<template>
<button @click="toNextPage">toNextPage</button>
</template>
<script setup lang="ts">
import {useRouter} from 'vue-router';
// 得到 router 对象
const router = useRouter();
// 页面跳转
const toNextPage = () => {
router.push({name: 'NextPage', params: {message: "基本字符串"}});
}
</script>
<template>
<h1>NextPage</h1>
<h1>基本数据:{{ route.params.message }}</h1>
</template>
<script setup lang="ts">
import { useRoute } from "vue-router";
import {onMounted} from "vue";
const route = useRoute();
</script>
<template>
<h1>NextPage</h1>
<h1 :key="thisVersion">基本数据:{{ route.params.message }}</h1>
</template>
<script setup lang="ts">
import { useRoute } from "vue-router";
import {ref, watch} from "vue";
const route = useRoute();
// 当前版本
const thisVersion = ref(0);
// 监听参数变化,更新版本,刷新组件(大致写法,未测试)
watch(() => route.params.message, () => {
thisVersion.value ++;
})
</script>
const routes: RouteRecordRaw[] = [
{
path: '/',
name: 'Hello',
component: Hello,
},
{
path: '/next-page',
name: 'NextPage',
component: NextPage,
},
];
<template>
<button @click="toNextPage">toNextPage</button>
</template>
<script setup lang="ts">
import {useRouter} from 'vue-router';
// 得到 router 对象
const router = useRouter();
// 页面跳转
const toNextPage = () => {
router.push({name: 'NextPage', query: {message: "基本字符串", name: "訾博"}});
}
</script>
<template>
<h1>NextPage</h1>
<h1>基本数据:{{ route.query.message }}</h1>
<h1>基本数据:{{ route.query.name }}</h1>
</template>
<script setup lang="ts">
import { useRoute } from "vue-router";
const route = useRoute();
</script>