|
| 1 | +import Vue from 'vue' |
| 2 | +import VueRouter from 'vue-router' |
| 3 | + |
| 4 | +Vue.use(VueRouter) |
| 5 | + |
| 6 | +const Home = { template: '<div>home {{$route.query.time}}</div>' } |
| 7 | +const Page = { template: '<div>page {{$route.query.time}}</div>' } |
| 8 | +const Detail = { template: '<div>detail {{$route.query.time}}</div>' } |
| 9 | + |
| 10 | +const router = new VueRouter({ |
| 11 | + mode: 'history', |
| 12 | + base: __dirname, |
| 13 | + routes: [ |
| 14 | + { path: '/', component: Home }, |
| 15 | + |
| 16 | + { path: '/page', component: Page }, |
| 17 | + |
| 18 | + { path: '/detail', component: Detail } |
| 19 | + |
| 20 | + ] |
| 21 | +}) |
| 22 | + |
| 23 | +// User can check the replace type in navigation guard, and do anything they want. |
| 24 | +router.beforeEach((to, from, next) => { |
| 25 | + if (to.replace) { |
| 26 | + to.query.replace = true |
| 27 | + } else { |
| 28 | + to.query.replace = false |
| 29 | + } |
| 30 | + |
| 31 | + if (to && to.query && !to.query.time) { |
| 32 | + to.query.time = new Date().getTime() |
| 33 | + next(to) |
| 34 | + } else { |
| 35 | + next() |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +new Vue({ |
| 40 | + router, |
| 41 | + template: ` |
| 42 | + <div id="app"> |
| 43 | + <h1>Push Or Replace</h1> |
| 44 | + <p>User can check the replace type in navigation guard, and do anything they want.</p> |
| 45 | + <pre> |
| 46 | +router.beforeEach((to, from, next) => { |
| 47 | + if (to.replace) { |
| 48 | + to.query.replace = true |
| 49 | + } |
| 50 | + else { |
| 51 | + to.query.replace = false |
| 52 | + } |
| 53 | + if (to && to.query && !to.query.time) { |
| 54 | + to.query.time = new Date().getTime() |
| 55 | + next(to) |
| 56 | + } else { |
| 57 | + next() |
| 58 | + } |
| 59 | +}) |
| 60 | + </pre> |
| 61 | + <ul> |
| 62 | + <li><router-link to="/">/</router-link></li> |
| 63 | + <li><router-link to="/page">/page</router-link> ( push )</li> |
| 64 | + <li><a @click="$router.push('/page')">/page</a> $router.push('/page') </li> |
| 65 | + <li><router-link to="/detail" replace>/detail</router-link> ( replace )</li> |
| 66 | + <li><a @click="$router.replace('/detail')">/detail</a> $router.replace('/detail') </li> |
| 67 | + <li><a @click="$router.go(-1)">back</a> $router.go(-1) </li> |
| 68 | + </ul> |
| 69 | + <router-view class="view"></router-view> |
| 70 | + </div> |
| 71 | + ` |
| 72 | +}).$mount('#app') |
0 commit comments