Vue.js 页面跳转有两种方式:路由跳转,适合复杂页面导航,使用 vue-router 管理,需要安装、创建路由表,并通过 router.push() 进行跳转。手动跳转,适合简单页面导航,可使用 window.location.assign()、this.$router.go() 或 window.open(),无需使用 vue-router,但要注意不会触发 Vue 路由钩子。

Vue 页面跳转详解
Vue.js 中页面跳转有两种主要方式:
一、路由跳转
路由跳转是一种更优雅、更灵活的跳转方式,适用于需要管理复杂页面导航的单页面应用程序 (SPA)。
立即学习“前端免费学习笔记(深入)”;
<code class="bash">npm install vue-router</code>
<code class="javascript">import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = new VueRouter({
routes
})</code><code class="vue"><template>
<button @click="goToAbout">Go to About</button>
</template>
<script>
import { useRouter } from 'vue-router'
export default {
setup() {
const router = useRouter()
const goToAbout = () => {
router.push('/about')
}
return {
goToAbout
}
}
}
</script></code>二、手动跳转
手动跳转适合简单页面导航或特殊情况。
window.location.assign()<code class="javascript">window.location.assign('https://example.com')</code>this.$router.go()<code class="javascript">this.$router.go(1) // 返回 this.$router.go(-1) // 前进</code>
window.open()<code class="javascript">window.open('https://example.com', '_blank') // 在新窗口中打开</code>注意事项:
beforeRouteEnter、beforeRouteLeave 和 beforeRouteUpdate 钩子。pushState 避免历史记录中出现丑陋的 URL。以上就是vue跳转页面怎么跳转的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号