路由绑定事件跳转页面,实现路由跳转,这里实现路由的前提是:编写在你从你开始跳转的页面的页面配置好事件,同时配置好router.js的路由
1、跳转到页面,首先得具有一个页面:登录页面
2、在主页中绑定一个点击事件:
2.1 绑定点击事件
import { createRouter, createWebHistory } from 'vue-router' // import HomeView from '../views/HomeView.vue' // import App from '@/App' // import registerView from '@/views/register/registerView' const routes = [ { path: '/', name: 'loginView', component: () => import('@/views/testView') // component: () => import('@/views/register/registerView') // component: () => import('@/views/login/loginView') }, { path: '/register', name: 'registerView', component: () => import('@/views/register/registerView') }, { path: '/login', name: 'homeView', component: () => import('@/views/HomeView') } // , // { // path: '/about', // name: 'about', // // route level code-splitting // // this generates a separate chunk (about.[hash].js) for this route // // which is lazy-loaded when the route is visited. // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue') // }, // { // path: '/reg', // component: () => import('@/views/register/registerView') // }, // { // path: '/login', // component: () => import('@/views/login/loginView') // }, ] const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes }) export default router
2.2 想要实现路由跳转,用router-view
3、this.$router.push('/login'),就可以实现页面跳转了,这里在router中的初始路径要不一样才可以哦
1. import { createRouter, createWebHistory } from 'vue-router' 2. // import HomeView from '../views/HomeView.vue' 3. // import App from '@/App' 4. // import registerView from '@/views/register/registerView' 5. 6. 7. 8. const routes = [ 9. { 10. path: '/', 11. name: 'loginView', 12. component: () => import('@/views/testView') 13. // component: () => import('@/views/register/registerView') 14. // component: () => import('@/views/login/loginView') 15. 16. }, 17. { 18. path: '/register', 19. name: 'registerView', 20. component: () => import('@/views/register/registerView') 21. }, 22. { 23. path: '/login', 24. name: 'homeView', 25. component: () => import('@/views/HomeView') 26. } 27. // , 28. // { 29. // path: '/about', 30. // name: 'about', 31. // // route level code-splitting 32. // // this generates a separate chunk (about.[hash].js) for this route 33. // // which is lazy-loaded when the route is visited. 34. // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue') 35. // }, 36. // { 37. // path: '/reg', 38. // component: () => import('@/views/register/registerView') 39. // }, 40. // { 41. // path: '/login', 42. // component: () => import('@/views/login/loginView') 43. // }, 44. 45. ] 46. 47. const router = createRouter({ 48. history: createWebHistory(process.env.BASE_URL), 49. routes 50. }) 51. 52. export default router
点击跳转,用router-link,相当于a链接:也可以实现跳转
<router-link to="/wisdom" active-class="ative">xxxxx</router-link>