【Vue】vue3.2配合vue-router4代码

vue3.2配合vue-router4代码

简单使用

npm导入

npm install vue-router@4

在src下创建router,再创建index.js

components下 创建两组件,home.vue和about.vue


index.js

import { createRouter,  createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/components/home.vue') // 建议进行路由懒加载,优化访问性能
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/components/about.vue')
  }
]

const router = createRouter({
  //history: createWebHistory(),    // 使用history模式
  history: createWebHashHistory(),	 // 使用hash模式
  routes
})

export default router

在main.js中引入并挂载

import { createApp } from 'vue'
import './style.css'
import router from './router/index.js'
import App from './App.vue'

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

App.vue引入

	<div id="nav">
    	<router-link to="/">Home</router-link> |
    	<router-link to="/about">about</router-link>
	</div>
	<router-view />

在按钮中点击后触发路由有些许不同

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter() //注意不要写错
const to = (res) => {
  router.push(res) //这里是字符型,也可以换成router.push({path:'/'})
}
</script>

<template>
  <!-- <div id="nav">
    	<router-link to="/">Home</router-link> |
    	<router-link to="/about">about</router-link>
	</div> -->
  <div>
    <button @click="to('/')">home</button>
    <button @click="to('/about')">a</button>

    <router-view />
  </div>
</template>

<style scoped>

</style>

onBeforeRouteLeave


可以替代组件内守卫,新增的组合式Api,离开当前页面路由时触发,return false则阻止跳转

举一个栗子

import { onBeforeRouteLeave } from 'vue-router'

export default {
  setup() {
    onBeforeRouteLeave((to, from) => {
      if (to.name !== 'home') {
          return false
      }
    })
  }
}

举第二个栗子

栗子2表示路由跳转之前进行判断是否要离开,确认的话调用next()离开当前页面,ElMessageBox.confirm是element-plus的一个方法

onBeforeRouteLeave((to, from, next) => {

      ElMessageBox.confirm("您还未付款,是否确认离开支付?", {

        confirmButtonText: "确定",

        cancelButtonText: "取消",

        type: "warning"

      }).then(() => {

          next();

        }).catch(() => {

          console.log("000");

        });

    });

onBeforeRouteUpdate


新增的组合式Api,路由更新时触发

import { onBeforeRouteUpdate } from 'vue-router'

export default {
  setup() {
    onBeforeRouteUpdate((to, from) => {
      if (to.params.id !== from.params.id) {
        console.log('......')
      }
    })
  }
}

onBeforeRouteLeaveonBeforeRouteUpdatevue-router提供的两个composition api,它们只能被用于setup中。


路由守卫语法变更

// 原写法
Router.beforeEach((to, from, next) => {
  if (ok) {
    next()
  } else {
    next(false)
  }
})

// 新写法
Router.beforeEach((to, from) => {
    if (ok) {
        console.log('...')
    } else {
        return false
    }
})

部分重大变更

  • 使用createRouter替换new Router()
  • mode: 'history'选项替换为更灵活的名称history
  • 带有空path的命名子路由不再添加斜线
Router3Router4
historycreateWebHistory()
hashcreateWebHashHistory()
abstractcreateMemoryHistory()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值