Skip to content

Commit 3c61830

Browse files
authored
feat: function watchQuery (nuxt#6245)
1 parent 7f542e0 commit 3c61830

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

packages/vue-app/template/client.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ async function render(to, from, next) {
279279
// Get route's matched components
280280
const matches = []
281281
const Components = getMatchedComponents(to, matches)
282+
const instances = getMatchedComponentsInstances(to)
282283

283284
// If no Components matched, generate 404
284285
if (!Components.length) {
@@ -371,6 +372,8 @@ async function render(to, from, next) {
371372
Component._dataRefresh = true
372373
} else if (Array.isArray(watchQuery)) {
373374
Component._dataRefresh = watchQuery.some(key => this._diffQuery[key])
375+
} else if (typeof watchQuery === 'function') {
376+
Component._dataRefresh = watchQuery.apply(instances[i], [to.query, from.query])
374377
}
375378
}
376379
if (!this._hadError && this._isMounted && !Component._dataRefresh) {
@@ -491,18 +494,13 @@ function showNextPage(to) {
491494
function fixPrepatch(to, ___) {
492495
if (this._pathChanged === false && this._queryChanged === false) return
493496

494-
const matches = []
495-
const instances = getMatchedComponentsInstances(to, matches)
496-
const Components = getMatchedComponents(to, matches)
497+
const instances = getMatchedComponentsInstances(to)
498+
const Components = getMatchedComponents(to)
497499

498500
Vue.nextTick(() => {
499501
instances.forEach((instance, i) => {
500502
if (!instance || instance._isDestroyed) return
501-
// if (
502-
// !this._queryChanged &&
503-
// to.matched[matches[i]].path.indexOf(':') === -1 &&
504-
// to.matched[matches[i]].path.indexOf('*') === -1
505-
// ) return // If not a dynamic route, skip
503+
506504
if (
507505
instance.constructor._dataRefresh &&
508506
Components[i] === instance.constructor &&

test/e2e/basic.browser.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ describe('basic browser', () => {
180180
page.close()
181181
})
182182

183+
test('/scroll-to-top in the same page with watchQuery function', async () => {
184+
const page = await browser.page(url('/scroll-to-top/watch-query-fn'))
185+
await page.evaluate(() => window.scrollBy(0, window.innerHeight))
186+
await page.nuxt.navigate('/scroll-to-top/watch-query-fn?other=1')
187+
let pageYOffset = await page.evaluate(() => window.pageYOffset)
188+
expect(pageYOffset).toBeGreaterThan(0)
189+
await page.nuxt.go(-1)
190+
pageYOffset = await page.evaluate(() => window.pageYOffset)
191+
expect(pageYOffset).toBeGreaterThan(0)
192+
await page.nuxt.navigate('/scroll-to-top/watch-query-fn?test=1')
193+
pageYOffset = await page.evaluate(() => window.pageYOffset)
194+
expect(pageYOffset).toBe(0)
195+
await page.nuxt.go(-1)
196+
pageYOffset = await page.evaluate(() => window.pageYOffset)
197+
expect(pageYOffset).toBe(0)
198+
page.close()
199+
})
200+
183201
test('/validate should display a 404', async () => {
184202
await page.nuxt.navigate('/validate')
185203

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<NuxtLink to="/scroll-to-top/watch-query-fn?test=1">
4+
go to the same page with other test
5+
</NuxtLink>
6+
<NuxtLink to="/scroll-to-top/watch-query-fn?other=1">
7+
go to the same page with param other
8+
</NuxtLink>
9+
</div>
10+
</template>
11+
12+
<script>
13+
export default {
14+
watchQuery (newQuery, oldQuery) {
15+
return newQuery.test
16+
}
17+
}
18+
</script>
19+
20+
<style>
21+
div {
22+
margin-top: 1500px;
23+
}
24+
</style>

0 commit comments

Comments
 (0)