Access router in a directive? #9488
Replies: 4 comments 1 reply
-
import { createApp } from 'vue'
import { useRoute } from 'vue-router'
const app = createApp({})
app.directive('focus', {
mounted(el) {
const route = useRoute()
console.log(route)
}
})
|
Beta Was this translation helpful? Give feedback.
-
I solved it by adding this in my directive file:
This is the file where createRouter is called. And then router is directly usable in the directive. |
Beta Was this translation helpful? Give feedback.
-
I'm sorry, I forgot that |
Beta Was this translation helpful? Give feedback.
-
Not sure how bad practice it is. But I'm passing router as param to directive
This way directive become more reusable between apps if you have monorepo |
Beta Was this translation helpful? Give feedback.
-
Is it possible to access the router from inside a mounted hook in a custom directive?
Beta Was this translation helpful? Give feedback.
All reactions