How to create a composable which returns modified path? #2000
-
Hey guys export const useAppRoute = () => {
const route = useRoute()
// { path: '/en-us/about' } -> { path: '/about' }
// I'm trying to get the path, without containing `params.lang`
return { ...route, path: route.path.slice(`/${route.params.lang}`.length) }
} Obviously this doesn't work This is the closest solution I could come up with export const useAppRoute = () => {
const route = useRoute()
const reactiveRoute = reactive(route)
watch(
route,
(newRoute) => {
Object.assign(reactiveRoute, newRoute, {
purePath: newRoute.path.slice(`/${newRoute.params.lang}`),
})
},
{ immediate: true, deep: true }
)
return reactiveRoute
} It seems that i can't write a set method for
Sorry guys, I'm not very familiar with proxy in js. Is there another approach you think might work better? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
the route object is readonly, so you can't do return computed(() => ({ ...route, path: route.path.slice(
route.indexOf('/', 1) // everything till the first /
) })) |
Beta Was this translation helpful? Give feedback.
the route object is readonly, so you can't do
reactive(useRoute())
orObject.assign(route, newRoute)
like you do. The first solution is closer to what you need to do, it's just not reactive but you could return a computed instead: