1

My component (Vue 3 with composition API + script setup) has a property linkList. This shall be updated whenever the route param id changes.

Example

When calling this component on /user/1, then linkList shall look like this:

{id: 1, name: 'foo'},
{id: 1, name: 'bar'}

This is my current code:

<script setup>

const route = useRoute();
const idRouteParam = computed(() => route.params.id);

const linkList = [
{
  id: idRouteParam,
  name: 'foo',
},
{
  id: idRouteParam,
  name: 'bar',
}
];

watch(
  idRouteParam,
  () => {
    console.log(idRouteParam);
  },
  { immediate: true },
);

</script>

The problem is: linkList is only updated after the page is refreshed, i.e. the component is re-created. When idRouteParam changes (example: /user/2), then linkList has the original values from user 1. I thought that I could update it in the watcher but I guess I'm missing some piece of the puzzle.

1 Answer 1

1

The linkList should be also defined as a computed property :

const linkList = computed(()=>[
{
  id: idRouteParam,
  name: 'foo',
},
{
  id: idRouteParam,
  name: 'bar',
}
]);

Sign up to request clarification or add additional context in comments.

2 Comments

OMG, sure... sometimes I'm thinking way too complicated 😂
sure, if a reactive property X is used as dependency in Y, Y should be a computed property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.