Closed luongthanhlam closed 6 years ago
generateUrls
uses the first path from array of paths which contains required :number(\\d+)
parameter in your example. You must provide the missed param or swap the paths.
url('user', { id: 11, slugs: 'abc', number: 22 }) // add `number` param
/* OR */
path: ['/:id(\\d+)-:slugs', '/:id(\\d+)-:slugs/page-:number(\\d+)'] // swap paths
When I swap paths, only the first part work
url('user', { id: 11, slugs: 'abc', number: 22 }) //=>/11-abc
Which the path url()
should generate?
Well, I fixed it by adding the missing param but I think generateUrls should uses all paths from array of paths, not only the first.
Could you provide an example what URL should be generated in your case then?
Oh yes, here is an example in my case:
url('user', { id: 11, slugs: 'abc' }) // => /11-abc
url('user', { id: 11, slugs: 'abc', number: 200 }) // => /11-abc/200
You can make the latest parameter optional:
const route = {
name: 'user',
path: '/:id(\\d+)-:slugs/:number?' // <== question mark
}
url('user', { id: 11, slugs: 'abc' }) // => /11-abc
url('user', { id: 11, slugs: 'abc', number: 200 }) // => /11-abc/200
Playground: http://jsfiddle.net/frenzzy/h3Lfz42q/ Read more: path-to-regexp parameters
Thank you, it works perfectly
When I call url('user', { id: 11, slugs: 'abc' }), it throw an error
Here are my routes.js: