Skip to content
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

Leaving the route causes the trigger parameter to change to undefined #86

Closed
awdr74100 opened this issue Nov 3, 2022 · 3 comments
Closed

Comments

@awdr74100
Copy link

awdr74100 commented Nov 3, 2022

Please forgive me if I put the question in the wrong place.

Based on the following routing structure:

├─ pages/
│   └─ posts
│         ├─ [id].vue
│         └─ index.vue
│   │
│   ├─ index.vue
│   └─ posts.vue

App.vue:

<template>
  <Navbar :paths="['/', '/posts']" />
  <RouterView />
</template>

<script setup lang="ts">
import { RouterView } from 'vue-router/auto';
import Navbar from '@/components/Navbar.vue';
</script>

<style scoped></style>

pages/posts.vue:

<template>
  <Navbar :paths="['/posts/1', '/posts/2', '/posts/3', '/posts/4']" />
  <RouterView />
</template>

<script setup lang="ts">
import { RouterView } from 'vue-router/auto';
import Navbar from '@/components/Navbar.vue';
</script>

<style scoped></style>

pages/posts/index.vue:

<script setup lang="ts">
import { useFetch } from '@vueuse/core';
import type { Post } from '@/types';

const url = `${import.meta.env.VITE_BACKEND_URL}/posts`;
const { isFetching, error, data: posts } = useFetch(url).get().json<Post[]>();
</script>

pages/posts/[id].vue:

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router/auto';
import { useFetch } from '@vueuse/core';
import type { Post } from '@/types';

const route = useRoute('/posts/[id]');

const url = ref(`${import.meta.env.VITE_BACKEND_URL}/posts/${route.params.id}`);
const {
  isFetching,
  error,
  data: post,
} = useFetch(url, { refetch: true }).get().json<Post>();

watch(
  () => route.params.id,
  (newId) => {
    console.log(newId); // Check

    if (newId === undefined) return;
    url.value = `${import.meta.env.VITE_BACKEND_URL}/posts/${newId}`;
  },
);
</script>

When routing from /posts/2 to /posts, why is watch() in [id].vue triggered and newId will be undefined? [id].vue seems to trigger watch() again before it is destroyed. Is this the expected behavior?

related discussion: stackoverflow
main problem: why does the watch() trigger when a user has navigated away from [id].vue?

Thank you for your help!

@posva
Copy link
Owner

posva commented Nov 3, 2022

This is relative to the router but in I couldn't reproduce it. It was a problem in the past and it got fixed so maybe you are using an outdated version?

I updated the reproductions just to make sure but it seems to work. But a normal watcher shouldn't trigger in that case

@posva posva closed this as completed Nov 3, 2022
@awdr74100
Copy link
Author

@posva Thank you for your very prompt response and help!!

Minimal reproduction:

Steps to reproduce:

  1. click /users/c button
  2. click /users button
  3. check the console (will show the undefined)

/users/c to / will not trigger watch(), /users/c to /users triggers watch(). It seems that watch() is triggered again before the component instance is destroyed, which seems to behave the same as the reuse component instance of /users/c to /users/b.

Explicitly defining routes does present the same problem. I think I will raise this problem again at vuejs/router, thank you for your help!!

@posva
Copy link
Owner

posva commented Nov 3, 2022

It's worth trying to reproduce it using only Vue. In the issue I mentioned I added reproductions with Vue. Maybe adding a nested component reproduces the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants