Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue3 跳转路由

作者:堕魂灭天   发布日期:2025-06-10   浏览:85

// 使用 Vue Router 在 Vue 3 中跳转路由的示例代码

// 首先,确保你已经安装并配置了 Vue Router
// 例如,在 main.js 中:
import { createApp } from 'vue';
import App from './App.vue';
import { createRouter, createWebHistory } from 'vue-router';
import Home from './views/Home.vue';
import About from './views/About.vue';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About }
  ]
});

createApp(App).use(router).mount('#app');

// 然后,在组件中使用 $router 进行路由跳转
// 例如,在一个按钮点击事件中跳转到关于页面:

<template>
  <div>
    <button @click="goToAbout">Go to About Page</button>
  </div>
</template>

<script>
export default {
  methods: {
    goToAbout() {
      // 使用 this.$router.push 方法进行路由跳转
      this.$router.push('/about');
    }
  }
}
</script>

// 解释说明:
// - `this.$router.push('/about')`:这是 Vue Router 提供的一个方法,用于导航到指定的路由路径。
// - `@click="goToAbout"`:这是一个事件绑定,当用户点击按钮时会触发 `goToAbout` 方法。

上一篇:vue2 teleport

下一篇:vue string转number

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站