uni-app框架路由:vue
npm install --save uni-vue-router
複製代碼
import Router, { Route, NextFn } from 'uni-vue-router';
Vue.use(Router);
const router = new Router();
new App({
router,
render: h => h(App),
}).$mount();
複製代碼
App.vue初始化最初的 $routevue-router
onLaunch(options) {
//first time init current route
this.$router.transitionTo({
path:options && options.path,
query:options && options.query
});
console.log('App Launch');
},
複製代碼
this.$router.push({
name:'bookings-detail',
path:'/pages/bookings/detail/index'
})
複製代碼
@Watch('$route',{
immediate: true
})function(newVal:Route,oldVal:Route) {
console.log('$route changed!',newVal,oldVal,this.$route)
}
複製代碼
router.beforeEach((to: Route, from: Route, next: NextFn) => {
console.log('[beforeEach] to from:', to, from);
if (to.name === 'bookings-detail') {
// return;
}
next();
});
router.beforeEach((to: Route, from: Route, next: NextFn) => {
console.log('[beforeEach]:', to, from, next);
next();
});
router.afterEach((to: Route, from: Route) => {
console.log('[afterEach] to from:', to, from);
});
router.afterEach((to: Route, from: Route) => {
console.log('[afterEach]:', to, from);
});
複製代碼
路由方法映射關係typescript
{
push: 'navigateTo',
pushTab: 'switchTab',
replace: 'redirectTo',
replaceAll: 'reLaunch',
back: 'navigateBack',
};
複製代碼
api reference vue-router APInpm
push(location: RawLocation, onComplete?: VoidFn, onAbort?: VoidFn)
push(location).then(onComplete).catch(onAbort)
pushTab(location: RawLocation, onComplete?: VoidFn, onAbort?: VoidFn)
beforeEach(beforeHook, onComplete?: VoidFn, onAbort?: VoidFn)
afterEach(afterHook, onComplete?: VoidFn, onAbort?: VoidFn)
transitionTo(location: RawLocation) //在 onTabItemTap以及onLaunch裏面 這種非手動調用的地方手動調用更新 $route
複製代碼
├── pages
│ ├── bookings
│ │ ├── detail
│ │ │ └── index.vue
│ │ ├── index.vue
複製代碼
[
{path: "/pages/bookings/detail/index",name:"bookings-detail"},
{path: "/pages/bookings/index", name: "bookings"}
]
複製代碼