feat(vue-app): support functional watchQuery (#6222)

This commit is contained in:
Xin Du (Clark) 2019-08-18 20:35:16 +01:00 committed by Pooya Parsa
parent 8f5244cabd
commit c6a9b37821
2 changed files with 14 additions and 8 deletions

View File

@ -140,15 +140,21 @@ async function loadAsyncComponents(to, from, next) {
<% } %>
try {
const Components = await resolveRouteComponents(to)
const Components = await resolveRouteComponents(
to,
(Component, instance) => ({ Component, instance })
)
<% if (loading) { %>
if (!this._pathChanged && this._queryChanged) {
// Add a marker on each component that it needs to refresh or not
const startLoader = Components.some((Component) => {
const startLoader = Components.some(({Component, instance}) => {
const watchQuery = Component.options.watchQuery
if (watchQuery === true) return true
if (Array.isArray(watchQuery)) {
if (watchQuery === true) {
return true
} else if (Array.isArray(watchQuery)) {
return watchQuery.some(key => this._diffQuery[key])
} else if (typeof watchQuery === 'function') {
return watchQuery.apply(instance, [to.query, from.query])
}
return false
})

View File

@ -98,15 +98,15 @@ export function flatMapComponents(route, fn) {
}))
}
export function resolveRouteComponents(route) {
export function resolveRouteComponents(route, fn) {
return Promise.all(
flatMapComponents(route, async (Component, _, match, key) => {
flatMapComponents(route, async (Component, instance, match, key) => {
// If component is a function, resolve it
if (typeof Component === 'function' && !Component.options) {
Component = await Component()
}
match.components[key] = sanitizeComponent(Component)
return match.components[key]
match.components[key] = Component = sanitizeComponent(Component)
return typeof fn === 'function' ? fn(Component, instance, match, key) : Component
})
)
}