Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue-router useRouter doesn't work when building library components

I am building a Vue3 npm component library with hopes that I could access the current router with vue-router's useRouter, and it would be automatically provided by whatever vue app is importing my library components.

If my library components are referenced directly import myCompThatUsesRouter from '../../myCompThatUsesRouter.vue the router works.

If I reference the same component via the node_module package import myCompThatUsesRouter from '@myPackage' router is undefined.

I also get a vue warning

injection "Symbol()" not found.

Is this not how these inject methods are intended to work?

like image 520
stackoverfloweth Avatar asked Jun 05 '26 09:06

stackoverfloweth


1 Answers

The issue was that my library defined vue-router as a "dependency", not a "peerDependency". https://nodejs.org/es/blog/npm/peer-dependencies/

Also my vite config needed to define vue-router as "external"

vite.config.ts

rollupOptions: {
   external: ['vue', 'vue-router']
}
like image 170
stackoverfloweth Avatar answered Jun 07 '26 23:06

stackoverfloweth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!