forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to check push vs replace in navigation guard
issue vuejs#1620 pull request vuejs#1906
- Loading branch information
1 parent
221e8b3
commit ff69c22
Showing
7 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
|
||
Vue.use(VueRouter) | ||
|
||
const Home = { template: '<div>home {{$route.query.time}}</div>' } | ||
const Page = { template: '<div>page {{$route.query.time}}</div>' } | ||
const Detail = { template: '<div>detail {{$route.query.time}}</div>' } | ||
|
||
const router = new VueRouter({ | ||
mode: 'history', | ||
base: __dirname, | ||
routes: [ | ||
{ path: '/', component: Home }, | ||
|
||
{ path: '/page', component: Page }, | ||
|
||
{ path: '/detail', component: Detail } | ||
|
||
] | ||
}) | ||
|
||
// User can check the replace type in navigation guard, and do anything they want. | ||
router.beforeEach((to, from, next) => { | ||
if (to.replace) { | ||
to.query.replace = true | ||
} else { | ||
to.query.replace = false | ||
} | ||
|
||
if (to && to.query && !to.query.time) { | ||
to.query.time = new Date().getTime() | ||
next(to) | ||
} else { | ||
next() | ||
} | ||
}) | ||
|
||
new Vue({ | ||
router, | ||
template: ` | ||
<div id="app"> | ||
<h1>Push Or Replace</h1> | ||
<p>User can check the replace type in navigation guard, and do anything they want.</p> | ||
<pre> | ||
router.beforeEach((to, from, next) => { | ||
if (to.replace) { | ||
to.query.replace = true | ||
} | ||
else { | ||
to.query.replace = false | ||
} | ||
if (to && to.query && !to.query.time) { | ||
to.query.time = new Date().getTime() | ||
next(to) | ||
} else { | ||
next() | ||
} | ||
}) | ||
</pre> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/page">/page</router-link> ( push )</li> | ||
<li><a @click="$router.push('/page')">/page</a> $router.push('/page') </li> | ||
<li><router-link to="/detail" replace>/detail</router-link> ( replace )</li> | ||
<li><a @click="$router.replace('/detail')">/detail</a> $router.replace('/detail') </li> | ||
<li><a @click="$router.go(-1)">back</a> $router.go(-1) </li> | ||
</ul> | ||
<router-view class="view"></router-view> | ||
</div> | ||
` | ||
}).$mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<a href="/">← Examples index</a> | ||
<div id="app"></div> | ||
<script src="/__build__/shared.chunk.js"></script> | ||
<script src="/__build__/push-or-replace.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ export function normalizeLocation ( | |
_normalized: true, | ||
path, | ||
query, | ||
hash | ||
hash, | ||
replace: !!next.replace | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters