Skip to content

Commit 4e828fb

Browse files
authored
[bugfix] Tabbar: avoid navigation duplicated (youzan#4147)
1 parent 1d673ca commit 4e828fb

4 files changed

Lines changed: 49 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"vue": "2.6.10",
110110
"vue-jest": "4.0.0-beta.2",
111111
"vue-loader": "^15.7.1",
112-
"vue-router": "^3.0.7",
112+
"vue-router": "^3.1.2",
113113
"vue-template-compiler": "2.6.10",
114114
"webpack": "4.39.1",
115115
"webpack-cli": "^3.3.6",

src/tabbar/test/index.spec.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1+
import VueRouter from 'vue-router';
12
import { mount, later } from '../../../test/utils';
23
import Vue from 'vue';
34
import Tabbar from '..';
45
import TabbarItem from '../../tabbar-item';
56

67
Vue.use(Tabbar);
78
Vue.use(TabbarItem);
9+
Vue.use(VueRouter);
810

911
test('route mode', async () => {
10-
Vue.util.defineReactive(Vue.prototype, '$route', { path: '/home' });
11-
12-
Vue.prototype.$router = {
13-
replace(to) {
14-
Vue.prototype.$route.path = typeof to === 'string' ? to : to.path;
15-
}
16-
};
17-
12+
const router = new VueRouter();
1813
const wrapper = mount({
14+
router,
1915
template: `
2016
<van-tabbar route>
21-
<van-tabbar-item replace to="/home">
17+
<van-tabbar-item replace to="/">
2218
Tab
2319
</van-tabbar-item>
2420
<van-tabbar-item replace to="/search">
@@ -48,6 +44,28 @@ test('route mode', async () => {
4844
expect(wrapper).toMatchSnapshot();
4945
});
5046

47+
test('router NavigationDuplicated', async done => {
48+
expect(async () => {
49+
const router = new VueRouter();
50+
const wrapper = mount({
51+
router,
52+
template: `
53+
<van-tabbar route>
54+
<van-tabbar-item replace to="/home">
55+
Tab
56+
</van-tabbar-item>
57+
</van-tabbar>
58+
`
59+
});
60+
61+
const item = wrapper.find('.van-tabbar-item');
62+
item.trigger('click');
63+
item.trigger('click');
64+
65+
await later();
66+
done();
67+
}).not.toThrow();
68+
});
5169

5270
test('watch tabbar value', () => {
5371
const wrapper = mount({
@@ -108,7 +126,10 @@ test('name prop', () => {
108126
}
109127
});
110128

111-
wrapper.findAll('.van-tabbar-item').at(1).trigger('click');
129+
wrapper
130+
.findAll('.van-tabbar-item')
131+
.at(1)
132+
.trigger('click');
112133

113134
expect(onChange).toHaveBeenCalledWith('b');
114135
});

src/utils/router.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ export type RouteConfig = {
1414
export function route(router: VueRouter, config: RouteConfig) {
1515
const { to, url, replace } = config;
1616
if (to && router) {
17-
router[replace ? 'replace' : 'push'](to);
17+
const promise = router[replace ? 'replace' : 'push'](to);
18+
19+
/* istanbul ignore else */
20+
if (promise && promise.catch) {
21+
promise.catch(err => {
22+
/* istanbul ignore if */
23+
if (err.name !== 'NavigationDuplicated') {
24+
throw err;
25+
}
26+
});
27+
}
1828
} else if (url) {
1929
replace ? location.replace(url) : (location.href = url);
2030
}
@@ -25,10 +35,10 @@ export function functionalRoute(context: RenderContext) {
2535
}
2636

2737
export type RouteProps = {
28-
url?: string,
38+
url?: string;
2939
replace?: boolean;
3040
to?: RawLocation;
31-
}
41+
};
3242

3343
export const routeProps = {
3444
url: String,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10089,10 +10089,10 @@ vue-loader@^15.7.1:
1008910089
vue-hot-reload-api "^2.3.0"
1009010090
vue-style-loader "^4.1.0"
1009110091

10092-
vue-router@^3.0.7:
10093-
version "3.0.7"
10094-
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.0.7.tgz#b36ca107b4acb8ff5bc4ff824584059c23fcb87b"
10095-
integrity sha512-utJ+QR3YlIC/6x6xq17UMXeAfxEvXA0VKD3PiSio7hBOZNusA1jXcbxZxVEfJunLp48oonjTepY8ORoIlRx/EQ==
10092+
vue-router@^3.1.2:
10093+
version "3.1.2"
10094+
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.1.2.tgz#2e0904703545dabdd42b2b7a2e617f02f99a1969"
10095+
integrity sha512-WssQEHSEvIS1/CI4CO2T8LJdoK4Q9Ngox28K7FDNMTfzNTk2WS5D0dDlqYCaPG+AG4Z8wJkn1KrBc7AhspZJUQ==
1009610096

1009710097
vue-style-loader@^4.1.0:
1009810098
version "4.1.2"

0 commit comments

Comments
 (0)