vue onBeforeUpdate works when route params change, but what about the first time?

  Kiến thức lập trình

My component looks like this so I can handle the first route load for a component and subsequent ones when the route param changes:

<script setup>

import {onBeforeRouteUpdate, useRoute} from "vue-router";
import {ref} from "vue";

const id = ref(useRoute().params.equipmentId)

onBeforeRouteUpdate(async (to, from) => {
  id.value = to.params.equipmentId
})

</script>

Is there a shorter, idiomatic way to do this so I don’t have to handle the “one off” case of getting the initial value?

LEAVE A COMMENT