Vue项目配置路由以及设置路由守卫的正确方式(无坑可食用)

简介: Vue项目配置路由以及设置路由守卫的正确方式(无坑可食用)

一、首先安装路由插件

cnpm install vue-router --save-dev

二、新建一个router文件夹,文件下新建一个index.js文件

37bbc4f414b742e799ea03ae43ffd296.png

三、index.js中引入router,代码如下

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export const constantRoutes = [
  {
    path: '/PixiDemo',
    component: () => import('@/components/PixiExample/PixiDemo.vue'),
    hidden: true
  },
  {
    path: '/PixiRope',
    component: () => import('@/components/PixiExample/PixiRope.vue'),
    hidden: true
  },
  { path: '*', redirect: '/PixiDemo', hidden: true }
]
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })
const router = createRouter()
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router

scrollBehavior 是为了每次跳转路由页面都是在最顶部

四、在根目录下创建promission.js文件,里面配置路由守卫

import Axios from 'axios'
import router from './router'
router.beforeEach(async(to, from, next) => {
  Axios.get(url).then(res => {
    localStorage.setItem('syslogin', res.data)
  }).catch(error => {
    // window.location.href = 'http://www.baidu.com'
    this.$message({
      message: '用户信息不正确',
      type: 'warning'
    })
    console.log(error)
  })
  next()
})
router.afterEach(() => {
  // finish progress bar
  // NProgress.done()
})

五、main.js引入

import router from './router'

import '@/permission'

new Vue({

 router,

 render: h => h(App)

}).$mount('#app')

import Vue from 'vue'
import App from './App.vue'
import 'cesium/Widgets/widgets.css'
import 'leaflet/dist/leaflet.css'
import 'leaflet-velocity/dist/leaflet-velocity.css'
import * as echarts from 'echarts'
import router from './router'
import '@/permission'
Vue.config.productionTip = false
Vue.prototype.$echarts = echarts
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')


相关文章
|
3月前
|
JavaScript 前端开发
如何在 Vue 项目中配置 Tree Shaking?
通过以上针对 Webpack 或 Rollup 的配置方法,就可以在 Vue 项目中有效地启用 Tree Shaking,从而优化项目的打包体积,提高项目的性能和加载速度。在实际配置过程中,需要根据项目的具体情况和需求,对配置进行适当的调整和优化。
|
3月前
|
JavaScript 前端开发 开发者
如何在 Visual Studio Code (VSCode) 中使用 ESLint 和 Prettier 检查代码规范并自动格式化 Vue.js 代码,包括安装插件、配置 ESLint 和 Prettier 以及 VSCode 设置的具体步骤
随着前端开发技术的快速发展,代码规范和格式化工具变得尤为重要。本文介绍了如何在 Visual Studio Code (VSCode) 中使用 ESLint 和 Prettier 检查代码规范并自动格式化 Vue.js 代码,包括安装插件、配置 ESLint 和 Prettier 以及 VSCode 设置的具体步骤。通过这些工具,可以显著提升编码效率和代码质量。
832 4
|
4月前
|
资源调度 JavaScript 前端开发
路由管理:Vue Router的使用和配置技巧
【10月更文挑战第21天】路由管理:Vue Router的使用和配置技巧
92 3
|
4月前
|
缓存 JavaScript 测试技术
Vue 代理设置
【10月更文挑战第14天】Vue 代理设置是 Vue 项目开发中非常重要的一个环节。通过合理的代理设置,我们可以解决跨域问题,提高开发效率和用户体验。
60 1
|
4月前
|
JavaScript API
vue 批量自动引入并注册组件或路由等等
【10月更文挑战第12天】 vue 批量自动引入并注册组件或路由等等
|
3月前
|
JavaScript UED
"Vue实战技巧大揭秘:一招解决路由跳转页面不回顶部难题,让你的单页面应用用户体验飙升!"
【10月更文挑战第23天】在Vue单页面应用中,点击路由跳转时,默认情况下页面不会自动滚动到顶部,这可能影响用户体验。本文通过一个新闻网站的案例,介绍了如何使用Vue-router的全局前置守卫和`scrollBehavior`方法,实现路由跳转时页面自动滚动到顶部的功能,提升用户浏览体验。
181 0
|
9月前
|
JavaScript API
【vue实战项目】通用管理系统:api封装、404页
【vue实战项目】通用管理系统:api封装、404页
91 3
|
9月前
|
人工智能 JavaScript 前端开发
毕设项目-基于Springboot和Vue实现蛋糕商城系统(三)
毕设项目-基于Springboot和Vue实现蛋糕商城系统
107 0
|
9月前
|
JavaScript Java 关系型数据库
毕设项目-基于Springboot和Vue实现蛋糕商城系统(一)
毕设项目-基于Springboot和Vue实现蛋糕商城系统
238 0
|
9月前
|
JavaScript 前端开发 API
Vue3+Vite+TypeScript常用项目模块详解
现在无论gitee还是github,越来越多的前端开源项目采用Vue3+Vite+TypeScript+Pinia+Elementplus+axios+Sass(css预编译语言等),其中还有各种项目配置比如eslint 校验代码工具配置等等,而我们想要进行前端项目的二次开发,就必须了解会使用这些东西,所以作者写了这篇文章进行简单的介绍。
178 0
Vue3+Vite+TypeScript常用项目模块详解

热门文章

最新文章