Usage of beforeRouteEnter with <script setup> #302
Replies: 13 comments 16 replies
-
At the moment there is a plan to support some kind of navigation guard that runs before entering the route and I started exploring it some time ago. It does require changes on Suspense and will come with an RFC for vue router once things are usable. Something like: setup() {
const user = ref()
// runs on each navigation
onBeforeResolve(async (to, from) => {
if (to.params.userId !== from.params.userId)
user.value = await getUsers(to.params.userId)
})
return { user }
} Note that at the moment there are way too many redundant ways to hook into the navigation cycle too. For instance, what you are doing could be done by using a global Update: the plan to support this kind of behavior can be found at #460 |
Beta Was this translation helpful? Give feedback.
-
The options api has three component navigation guards:
and they all appear in the same location in the component code. The composition api implements the same three component guards, but with different names:
The big change is that the
The other two can be defined in script setup. |
Beta Was this translation helpful? Give feedback.
-
That's not an ideal solution because it requires beforeEnter logic to be specified in the route definition, not the component definition. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? As a workaround, we may implement a navigation guard inside route definition, using imported store data. But that's not the case when we should put the guard logic right inside the component and validate the route based on the component's data. |
Beta Was this translation helpful? Give feedback.
-
Another option would be to use both For instance, this seems to work. I'm not sure if there are any pitfalls, but I thought it was worth mentioning. <script>
import { useFetch } from '@vueuse/core'
export default {
beforeRouteEnter: function (to) {
const { data } = useFetch(`/your/url/${to.params.id}`)
to.meta.data = data
}
}
</script>
<script setup>
defineProps({
data: {
type: [null, Object],
required: true
}
})
// ...
</script> and then add a {
path: '/your/url/:id',
// ...
props: to => ({ data: to.meta.data })
} |
Beta Was this translation helpful? Give feedback.
-
any news on this please? 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Referring to @pau-santesmasses 's answer, the typescript way should be: <script lang="ts">
import { defineComponent, ComponentPublicInstance } from 'vue'
interface IInstance extends ComponentPublicInstance {
setPathFrom(from: string): void
}
export default defineComponent({
beforeRouteEnter(to, from, next) {
next((vm) => {
const instance = vm as IInstance
instance.setPathFrom(from.path)
})
},
})
</script>
<script lang="ts" setup>
let pathFrom: string
const setPathFrom = (path: string) => {
pathFrom = path
console.log('vue-route::from::', pathFrom)
}
defineExpose({ setPathFrom })
</script> |
Beta Was this translation helpful? Give feedback.
-
For me this only works if use inside a view and not in component: |
Beta Was this translation helpful? Give feedback.
-
Thanks all. |
Beta Was this translation helpful? Give feedback.
-
Instead of using
docs: https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes |
Beta Was this translation helpful? Give feedback.
-
Hi @posva, is there an update on how to do this yet? I'll appreciate any help on this. |
Beta Was this translation helpful? Give feedback.
-
Closing in favor of #460 because of the points mentioned in comments above. |
Beta Was this translation helpful? Give feedback.
-
Currently with the composition API the beforeRouteEnter hook needs to be defined outside the setup block:
My understanding is that this is because the
beforeRouteEnter
hook has to run before thesetup
function. My question is - is there a plan for supporting this with<script setup>
? I can't seem to find any information in the RFC or in the discussion board.Beta Was this translation helpful? Give feedback.
All reactions