Skip to content

merge dev into patch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,4 @@ These properties are injected into every child component by passing the router i
- **beforeRouteUpdate**
- **beforeRouteLeave**

See [In Component Guards](../guide/advanced/navigation-guards.md#incomponent-guards).
See [In Component Guards](../guide/advanced/navigation-guards.md#in-component-guards).
4 changes: 2 additions & 2 deletions docs/zh/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sidebar: auto
</router-link>
```

在这种情况下,`<a>` 将作为真实的链接 (它会获得正确的 `href` 的),而 "激活时的CSS类名" 则设置到外层的 `<li>`。
在这种情况下,`<a>` 将作为真实的链接 (它会获得正确的 `href` 的),而 "激活时的 CSS 类名" 则设置到外层的 `<li>`。

## `<router-link>` Props

Expand Down Expand Up @@ -332,7 +332,7 @@ router.forward()
const matchedComponents: Array<Component> = router.getMatchedComponents(location?)
```

返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类,不是实例)。通常在服务端渲染的数据预加载时时候
返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类,不是实例)。通常在服务端渲染的数据预加载时使用

### router.resolve

Expand Down
20 changes: 16 additions & 4 deletions examples/nested-routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ const Quy = {
</div>
`
}
const Quux = { template: '<div>quux</div>' }
const Zap = { template: '<div><h3>zap</h3><pre>{{ $route.params.zapId }}</pre></div>' }
const Quux = {
template: `<div>quux<router-link :to="{ name: 'quuy' }">go to quuy</router-link></div>`
}
const Quuy = { template: '<div>quuy</div>' }
const Zap = {
template: '<div><h3>zap</h3><pre>{{ $route.params.zapId }}</pre></div>'
}

const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/', redirect: '/parent' },
{ path: '/parent', component: Parent,
{
path: '/parent',
component: Parent,
children: [
// an empty path will be treated as the default, e.g.
// components rendered at /parent: Root -> Parent -> Default
Expand All @@ -65,7 +72,10 @@ const router = new VueRouter({
{
path: 'qux/:quxId',
component: Qux,
children: [{ path: 'quux', name: 'quux', component: Quux }]
children: [
{ path: 'quux', name: 'quux', component: Quux },
{ path: 'quuy', name: 'quuy', component: Quuy }
]
},

{ path: 'quy/:quyId', component: Quy },
Expand All @@ -91,6 +101,8 @@ new Vue({
<li><router-link :to="{name: 'zap'}">/parent/zap</router-link></li>
<li><router-link :to="{name: 'zap', params: {zapId: 1}}">/parent/zap/1</router-link></li>
<li><router-link :to="{ params: { zapId: 2 }}">{ params: { zapId: 2 }} (relative params)</router-link></li>
<li><router-link to="/parent/qux/1/quux">/parent/qux/1/quux</router-link></li>
<li><router-link to="/parent/qux/2/quux">/parent/qux/2/quux</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ export default class VueRouter {
normalizedTo: Location,
resolved: Route
} {
current = current || this.history.current
const location = normalizeLocation(
to,
current || this.history.current,
current,
append,
this
)
Expand Down
4 changes: 3 additions & 1 deletion src/util/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export function normalizeLocation (
): Location {
let next: Location = typeof raw === 'string' ? { path: raw } : raw
// named target
if (next.name || next._normalized) {
if (next._normalized) {
return next
} else if (next.name) {
return extend({}, raw)
}

// relative params
Expand Down
9 changes: 8 additions & 1 deletion test/e2e/specs/nested-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
browser
.url('http://localhost:8080/nested-routes/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 9)
.assert.count('li a', 11)
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'default')
Expand Down Expand Up @@ -70,6 +70,13 @@ module.exports = {
return (zapId === '2')
}, null, 'relative params')

.click('li:nth-child(10) a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/1/quux')
.click('li:nth-child(11) a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quux')
.click('.nested-child a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quuy')

// check initial visit
.url('http://localhost:8080/nested-routes/parent/foo')
.waitForElementVisible('#app', 1000)
Expand Down
40 changes: 40 additions & 0 deletions test/unit/specs/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ describe('router.onReady', () => {
})
})

describe('route matching', () => {
it('resolves parent params when using current route', () => {
const router = new Router({
mode: 'abstract',
routes: [
{
path: '/a/:id',
component: { name: 'A' },
children: [{ name: 'b', path: 'b', component: { name: 'B' }}]
}
]
})

router.push('/a/1')

const { route, resolved } = router.resolve({ name: 'b' })
expect(route.params).toEqual({ id: '1' })
expect(resolved.params).toEqual({ id: '1' })
})

it('can override currentRoute', () => {
const router = new Router({
mode: 'abstract',
routes: [
{
path: '/a/:id',
component: { name: 'A' },
children: [{ name: 'b', path: 'b', component: { name: 'B' }}]
}
]
})

router.push('/a/1')

const { route, resolved } = router.resolve({ name: 'b' }, { params: { id: '2' }, path: '/a/2' })
expect(route.params).toEqual({ id: '2' })
expect(resolved.params).toEqual({ id: '2' })
})
})

describe('router.addRoutes', () => {
it('should work', () => {
const router = new Router({
Expand Down
9 changes: 9 additions & 0 deletions test/unit/specs/location.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,14 @@ describe('Location utils', () => {
const loc2 = normalizeLocation(loc1)
expect(loc1).toBe(loc2)
})

it('creates copies when not normalized', () => {
const l1 = { name: 'foo' }
expect(normalizeLocation(l1)).not.toBe(l1)
const l2 = { path: '/foo' }
expect(normalizeLocation(l2)).not.toBe(l2)
const l3 = { path: '/foo', query: { foo: 'foo' }}
expect(normalizeLocation(l3)).not.toBe(l3)
})
})
})
Loading