Skip to content

Commit 3c2f67b

Browse files
committed
ADD:路由卫士
1 parent 31d95d7 commit 3c2f67b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import router from './router';
44
import store from './store';
55

66
import { Component } from 'vue-property-decorator';
7+
import { Route } from 'vue-router';
78

89
Component.registerHooks([
910
'beforeRouteEnter',
@@ -13,6 +14,10 @@ Component.registerHooks([
1314

1415
Vue.config.productionTip = false;
1516

17+
router.beforeEach((to: Route, from: Route, next: () => void) => {
18+
console.log(to, from);
19+
next();
20+
});
1621
new Vue({
1722
router,
1823
store,

src/pages/about/About.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<div class="about">
33
<h1>This is an about page</h1>
4+
<button @click="goBack">go back</button>
45
</div>
56
</template>
67

@@ -17,7 +18,7 @@ export default class About extends Vue {
1718
1819
private beforeRouteEnter(to: Route, from: Route, next: () => void): void {
1920
console.log('beforeRouteEnter', to, from, next);
20-
next(); // 没有next将不会进入路由内部,跟vue文档用法一致
21+
next();
2122
}
2223
2324
private beforeRouteUpdate(to: Route, from: Route, next: () => void): void {
@@ -29,6 +30,10 @@ export default class About extends Vue {
2930
console.log('beforeRouteLeave');
3031
next();
3132
}
33+
34+
private goBack() {
35+
this.$router.back();
36+
}
3237
}
3338
3439
</script>

0 commit comments

Comments
 (0)