当前位置:首页 > VUE

用vue实现界面跳转

2026-01-20 08:58:53VUE

路由配置

在Vue项目中实现界面跳转通常使用Vue Router。确保已安装Vue Router:

npm install vue-router

src/router/index.js中配置路由:

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

路由挂载

main.js中挂载路由:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

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

声明式导航

在模板中使用<router-link>实现跳转:

<template>
  <router-link to="/">Home</router-link>
  <router-link to="/about">About</router-link>
</template>

编程式导航

在方法中使用router.push()实现跳转:

methods: {
  goToAbout() {
    this.$router.push('/about')
  }
}

动态路由

配置带参数的路由:

const routes = [
  { path: '/user/:id', component: User }
]

跳转到动态路由:

用vue实现界面跳转

this.$router.push('/user/123')

获取路由参数:

this.$route.params.id

路由守卫

全局前置守卫:

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    // 验证逻辑
  } else {
    next()
  }
})

组件内守卫:

beforeRouteEnter(to, from, next) {
  // 在渲染该组件的对应路由被验证前调用
  next()
}

命名路由

配置命名路由:

用vue实现界面跳转

const routes = [
  { path: '/user/:id', name: 'user', component: User }
]

通过名称跳转:

this.$router.push({ name: 'user', params: { id: 123 } })

路由传参

通过props传递参数:

const routes = [
  { path: '/user/:id', component: User, props: true }
]

在组件中接收:

props: ['id']

嵌套路由

配置嵌套路由:

const routes = [
  {
    path: '/user',
    component: User,
    children: [
      { path: 'profile', component: Profile }
    ]
  }
]

路由懒加载

实现组件懒加载:

const routes = [
  { path: '/about', component: () => import('../views/About.vue') }
]

标签: 跳转界面
分享给朋友:

相关文章

h5实现登录页面跳转页面跳转页面跳转

h5实现登录页面跳转页面跳转页面跳转

使用HTML5实现登录页面跳转 在HTML5中实现登录页面跳转可以通过多种方式完成,包括表单提交、JavaScript跳转和超链接跳转。以下是几种常见的实现方法。 表单提交跳转 使用HTML表单的a…

h5实现页面跳转页面跳转页面

h5实现页面跳转页面跳转页面

H5 实现页面跳转的方法 使用 <a> 标签实现跳转 通过超链接标签 <a> 的 href 属性指定目标页面路径,用户点击后跳转。 <a href="target.…

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 ro…

实现js页面跳转

实现js页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现跳转,这是最常用的方法: window.location.href = "https://…

elementui界面

elementui界面

ElementUI 界面设计指南 ElementUI 是一套基于 Vue.js 的桌面端组件库,适合快速构建现代化的后台管理系统或企业级应用界面。以下是关键设计方法和组件使用示例: 布局与容器 使用…

js怎么实现网页跳转页面跳转页面跳转

js怎么实现网页跳转页面跳转页面跳转

JavaScript 实现网页跳转的方法 使用 window.location.href 跳转 通过修改 window.location.href 属性实现页面跳转,这是最常用的方式。例如: w…