说明
Fork from:@ljhmp/uni-router-vue3 (https://gitee.com/ljhmp/uni-router-vue3)
修改:接收参数添加success、fail回调的配置
为uniapp写的路由库,实现类似vue-router的效果,仅支持vue3(兼容nvue)
使用方法
配置及初始化
import { createRouter } from '@ljhmp/uni-router-vue3'
import pageJson from '@/pages.json'
const router = createRouter({
pageJson
})
export default router
import { createSSRApp } from 'vue'
import router from './router'
import App from './App.vue'
export function createApp() {
const app = createSSRApp(App)
app.use(router)
return {
app
}
}
路由跳转方法
router.push
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
const router = useRouter()
const route = useRoute()
router.push('/pages/home/home')
router.push('home')
router.push({
path: '',
name: '',
delay: 500,
query: {},
params: {},
ignoreGuard: false,
animationType: '',
animationDuration: 300,
events: any,
success: res => {},
fail: res => {},
})
router.tab、router.replace、router.reLaunch
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
const router = useRouter()
const route = useRoute()
router.tab('/pages/home/home')
router.replace('home')
router.reLaunch({
path: '',
name: '',
delay: 500,
query: {},
params: {},
ignoreGuard: false,
})
router.back
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
const router = useRouter()
const route = useRoute()
router.back(500)
router.back(2)
router.back()
router.back({
delta: 1,
delay: 500,
query: {},
params: {},
ignoreGuard: false,
animationType: '',
animationDuration: 300,
})
router.go
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
const router = useRouter()
const route = useRoute()
router.go('/pages/home/home')
router.go('home')
router.go({
type: 'push' | 'tab' | 'replace' | 'reLaunch' | 'back',
})
router.beforeEach、router.afterEach
router.beforeEach(async (to, from) => {
if (to.path.includes('my')) {
await delay(500)
to.query.type = 'my'
} else if (to.path.includes('about')) {
to.query.type = 'about'
}
})
router.beforeEach((to, from, next) => {
console.log(to)
if (to.path.includes('about')) {
setTimeout(() => {
next({ name: 'my' })
}, 100)
} else {
next()
}
})
router.afterEach((to, from) => {
progress.done()
})
route、to、from 类型
export type RouteRaw = {
path: string
url: string
name?: string
}
export type Route = RouteRaw & {
query: AnyObject
params: AnyObject
type: NavType
method: NavMethodType
ignoreGuard?: boolean
delay?: number
from?: string
}
设置|获取 页面参数:router.getParams、router.setParams、useParams
- 适用于跨多页面传递数据,与页面的params没有关系
import { useRouter, useParams } from "@ljhmp/uni-router-vue3";
const router = useRouter()
router.setParams('about', { a: 1 })
router.getParams('about')
const { params, setParams } = useParams('about')
setParams({a: 1})
console.log(params)
getParams(key: string, isDel?: boolean): any
useParams(key: string, isDel?: boolean)
setParams(key: string, value: object, type?: 'rewrite' | 'merge'): void
使用事例
- 传递上个页面的数据及方法,在新的页面进行更新或使用
<template>
<view>
<text>{{ count }}</text>
<button @click="goAbout">去about</button>
</view>
</template>
<script lang="ts" setup>
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
import { ref } from "vue";
const router = useRouter()
const route = useRoute()
const count = ref(0)
const addCount = (value: number = 1) => {
count.value = count.value + value
}
const goAbout = () => {
router.push({
name: 'about',
params: {a: 1, count, addCount},
query: {b: 2},
delay: 200,
})
}
</script>
<template>
<view>
<text>{{ count }}</text>
<text>{{ route.params.count }}</text>
<button @click="addCount">count+5</button>
<button @click="route.params.addCount()">count+1</button>
</view>
</template>
<script lang="ts" setup>
import { useRouter, useRoute } from "@ljhmp/uni-router-vue3";
import { computed } from "vue";
const router = useRouter()
const route = useRoute()
const count = computed(() => route.params.count)
const addCount = () => {
route.params.addCount(5)
}
</script>