当前位置: 首页 > Web前端 > vue.js

基本vue路由配置

时间:2023-03-31 18:06:34 vue.js

Vue是单页面应用,所有的页面跳转都是通过路由实现的。实现如下路由配置:{routingfile:index.jsfileinrouter}importVuefrom'vue'importVueRouterfrom'vue-router'Vue.use(VueRouter)引入并使用路由consthome=()=>import('../views/home-index.vue');//我们先引入一个路由页面{使用路由懒加载,和图片懒加载的原理类似,只有跳转的时候才会加载,防止第一次进入时耗时过长}constroutes=[{path:'/',name:'home',component:home,},{path:'/signIn',name:'signIn',component:signIn,meta:{title:'login'},]//这里我配置了两条路由{不是全部导入}})exportdefaultrouterfirst开始引入属性:path:这是地址栏显示的值{Example:www.taobao.com/用户。这里的user就是路径}name:一个名字,类似于给这个路由加一个id。在组件中使用路由跳转时,会使用{programmaticnavigation}。像window.open这样的组件:这个值比较关键组件的名字就是组件的名字,必须和引入的时候的值对应。比如path:user在实际开发user对应的组件时可能会遇到一些问题;想切换部分首页路由怎么办?在登录页面和注册页面之间交换路由怎么样?路由也提供了子模式constroutes=[{path:'/',name:'home',component:home,children:[{//subcomponentofhomepath:'',name:'homeIndex',component:homeIndex},{path:'/user',name:'user',component:user,}]}]我们创建一个子数组,在数组中创建路由对图像没问题,在组件中依然正常跳转constrouter=newVueRouter({//mode:'history',//取消历史模式//base:'/dist',routes})