File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -146,4 +146,21 @@ describe('warnings', () => {
146
146
await expect ( router . push ( { path : '//not-valid' } ) ) . resolves . toBe ( undefined )
147
147
expect ( 'cannot start with multiple slashes' ) . toHaveBeenWarned ( )
148
148
} )
149
+
150
+ it ( 'warns if path contains the same param multiple times' , ( ) => {
151
+ const history = createMemoryHistory ( )
152
+ createRouter ( {
153
+ history,
154
+ routes : [
155
+ {
156
+ path : '/:id' ,
157
+ component,
158
+ children : [ { path : ':id' , component } ] ,
159
+ } ,
160
+ ] ,
161
+ } )
162
+ expect (
163
+ 'duplicated params with name "id" for path "/:id/:id"'
164
+ ) . toHaveBeenWarned ( )
165
+ } )
149
166
} )
Original file line number Diff line number Diff line change 5
5
PathParserOptions ,
6
6
} from './pathParserRanker'
7
7
import { tokenizePath } from './pathTokenizer'
8
+ import { warn } from '../warning'
8
9
9
10
export interface RouteRecordMatcher extends PathParser {
10
11
record : RouteRecord
@@ -20,6 +21,19 @@ export function createRouteRecordMatcher(
20
21
options ?: PathParserOptions
21
22
) : RouteRecordMatcher {
22
23
const parser = tokensToParser ( tokenizePath ( record . path ) , options )
24
+
25
+ // warn against params with the same name
26
+ if ( __DEV__ ) {
27
+ const existingKeys = new Set < string > ( )
28
+ for ( const key of parser . keys ) {
29
+ if ( existingKeys . has ( key . name ) )
30
+ warn (
31
+ `Found duplicated params with name "${ key . name } " for path "${ record . path } ". Only the last one will be available on "$route.params".`
32
+ )
33
+ existingKeys . add ( key . name )
34
+ }
35
+ }
36
+
23
37
const matcher : RouteRecordMatcher = {
24
38
...parser ,
25
39
record,
You can’t perform that action at this time.
0 commit comments