2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-23 00:10:35 +03:00

feat: add afterNavigation callback (fix: #259)

This commit is contained in:
pimlie
2019-03-05 15:11:25 +01:00
parent 173b31d1d7
commit 97badf61cb
6 changed files with 48 additions and 7 deletions
+18
View File
@@ -657,6 +657,24 @@ Will be called when the client `metaInfo` updates/changes. Receives the followin
Default `false`. If set to `true` then vue-meta will pause updating `metaInfo` during page navigation and only refresh once when navigation has finished. It does this by adding a global beforeEach and afterEach navigation guard on the vue-router instance. Default `false`. If set to `true` then vue-meta will pause updating `metaInfo` during page navigation and only refresh once when navigation has finished. It does this by adding a global beforeEach and afterEach navigation guard on the vue-router instance.
#### `afterNavigation` (Function)
Will be called when the client `metaInfo` has changed after navigation occured. Receives the following parameters:
- `newInfo` (Object) - The new state of the `metaInfo` object.
> :warning: This option only works when `refreshOnceOnNavigation: true`. Please see the [vue-router example](./examples/vue-router)
`this` context is the component instance `afterNavigation` is defined on.
```js
{
metaInfo: {
afterNavigation (newInfo) {
console.log('Meta info update finished after navigation!')
}
}
}
```
### How `metaInfo` is Resolved ### How `metaInfo` is Resolved
+11 -3
View File
@@ -7,18 +7,26 @@ Vue.use(VueMeta, {
refreshOnceOnNavigation: true refreshOnceOnNavigation: true
}) })
let metaUpdated = 'no'
const ChildComponent = { const ChildComponent = {
name: `child-component`, name: `child-component`,
props: ['page'], props: ['page'],
template: `<h3>You're looking at the <strong>{{ page }}</strong> page</h3>`, template: `<div>
<h3>You're looking at the <strong>{{ page }}</strong> page</h3>
<p>Has metaInfo been updated? {{ metaUpdated }}</p>
</div>`,
metaInfo () { metaInfo () {
return { return {
title: `${this.page} - ${this.date && this.date.toTimeString()}` title: `${this.page} - ${this.date && this.date.toTimeString()}`,
afterNavigation() {
metaUpdated = 'yes'
}
} }
}, },
data() { data() {
return { return {
date: null date: null,
metaUpdated
}; };
}, },
mounted() { mounted() {
+1 -1
View File
@@ -30,6 +30,6 @@ export default function _refresh(options = {}) {
metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags) metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags)
} }
return metaInfo return { vm: this, metaInfo, tags }
} }
} }
+6 -1
View File
@@ -81,7 +81,12 @@ export default function createMixin(Vue, options) {
next() next()
}) })
$router.afterEach(() => $rootMeta.resume()) $router.afterEach(() => {
const { vm, metaInfo } = $rootMeta.resume()
if (metaInfo && metaInfo.afterNavigation && isFunction(metaInfo.afterNavigation)) {
metaInfo.afterNavigation.call(vm, metaInfo)
}
})
} }
} }
} }
+11 -1
View File
@@ -1,4 +1,4 @@
import { isObject } from './typeof' import { isObject, isFunction } from './typeof'
import { import {
keyName, keyName,
@@ -29,5 +29,15 @@ export default function setOptions(options) {
} }
} }
if (options.afterNavigation && !isFunction(options.afterNavigation)) {
console.warn(`afterNavigation should be a function, received ${typeof options.afterNavigation} instead`) // eslint-disable-line no-console
options.afterNavigation = void 0
return options
}
if (options.afterNavigation && !options.refreshOnceOnNavigation) {
options.refreshOnceOnNavigation = true
}
return options return options
} }
+1 -1
View File
@@ -100,7 +100,7 @@ describe('plugin', () => {
expect(batchUpdateSpy).not.toHaveBeenCalled() expect(batchUpdateSpy).not.toHaveBeenCalled()
jest.clearAllMocks() jest.clearAllMocks()
const metaInfo = wrapper.vm.$meta().resume() const { metaInfo } = wrapper.vm.$meta().resume()
expect(metaInfo.title).toBe(title) expect(metaInfo.title).toBe(title)
}) })
}) })