router.push('xxx')的时候一直跳转到 / 路径,但是使用<router-link :to="toPage2()"></router-link>这种标签就可以正常跳转。
vue3 + vue-router4
<template>
<h1>page1</h1>
<button v-on:click="toPage2()">toPage2</button>
</template>
<script>
import {useRouter} from 'vue-router'
export default {
name: "page1",
setup () {
const router = useRouter()
const toPage2 = () => {
router.push('page2')
}
return {
toPage2
}
}
}
</script>
<template>
<h2>page2</h2>
<button v-on:click="toPage1()">toPage1</button>
</template>
<script>
import {useRouter} from "vue-router";
export default {
name: "page2",
setup () {
const router = useRouter()
const toPage1 = () => {
router.push('page1')
}
return {
toPage1
}
}
}
</script>
import {createRouter, createWebHistory} from 'vue-router'
const routes = [
{
path: "/",
redirect: 'page1'
},
{
path: '/page1',
name: 'page1',
component: () => import('@/view/page1.vue')
},
{
path: '/page2',
name: 'page2',
component: () => import('@/view/page2.vue')
},
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
<template>
<router-view/>
</template>
<script>
export default {
name: 'App',
}
</script>
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.