vue-router

本文详细介绍了Vue Router的基本配置,包括安装、路由定义及在main.js中的应用。深入讲解了路由跳转,通过`router-link`和编程式导航实现页面切换。动态路由的两种方式展示了如何传递参数。嵌套路由的使用让组件结构更加清晰。同时,还提及了导航守卫的概念,用于在路由变化时执行特定逻辑。最后,提到了路由懒加载以优化应用性能。
摘要由CSDN通过智能技术生成

vue-router

  • 路由基本配置

  • 路由跳转
    router-link
    编程式导航

  • 动态路由

  • 嵌套路由

  • 导航守卫

  • 路由懒加载

基本路由配置
1.安装router
2.在router下的index.js中配置

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routers = [
	{
    	path: '/',
	    name: 'Home',
   		redirect:'/price',	//重定向
   		component: () => import('../components/price/PriceEdit.vue'),	//路由懒加载
	  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

3.在main.js中调用index.js

import router from './router'
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

** 路由跳转 (router-link 编程式导航) **
router-link

<router-link to="/home">Home</router-link>	//跳转
<router-view/>		//展示

编程式导航

this.$router.push({path:'/home'})

动态路由
方式一
-------操作路由用$router 获取路由参数用route
-------query 要搭配path路径,query像get请求
-------params只能由命名路由name来引入,params像post请求

<p>{{$route.params.id}}</p>

this.$router.push({path:'/home', query:{name:'deng'}})
this.$router.push({name:'home', params:{id:1}})

方式二

{
    path: '/price/id:1',
    name: 'Price',
    component: () => import('../components/price/Price.vue'),
  },

嵌套路由

{
    path: '/',
    name: 'Home',
    children:[{
      path: '/index',
      name: 'index',
      component: () => import('../components/index.vue'),
    }]
  },

导航守卫
router.beforeEach((to,from,next)

router.beforeEach((to,from,next)=>{
  // beforeEach(去向,来自,执行下一步)
  console.log(to.path)
  next()
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值