Help to create basic Vue3 plugin with nested components #9436
-
I'm trying to build a Vue3 plugin but I faced some bugs and need help, please. The bug is: nested components of the plugin are not resovled by Vue when they are imported from node modules in another application. In order to reproduce this bug, I created a demo app and a demo plugin: The repositories contain instructions to set up the plugin and the demo app (very basic stuff, just run a few commands). By running the demo app as specified, it is possible to see that the inner components of the plugin are not resolved by Vue, so they are not rendered neither mounted. If more details are missing, just ask. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
I think was a mistake from my part not having provided enough description, just the url of the app. So, here is what is going on:
Vue does not render Here is the HTML displayed in the browser: <div id="app" data-v-app="">
<h1>Vue3 Plugin Demo App</h1>
<p>This is the demo
app.</p>
<div>
<h1>This is the outer component</h1>
<myinnercomponent msg="awesome, it works!"></myinnercomponent>
</div>
</div> Here is <template>
<h1>Vue3 Plugin Demo App</h1>
<p>This is the demo app.</p>
<my-outer-component msg="awesome, it works!" />
</template>
<script>
export default {
name: 'App'
}
</script> Here is import { createApp } from 'vue'
import plugin from 'vue3-plugin'
import App from './App.vue'
const app = createApp(App)
app.use(plugin)
app.mount('#app') Here is <template>
<div>
<h1>This is the outer component</h1>
<MyInnerComponent :msg="msg" />
</div>
</template>
<script>
import { defineComponent } from 'vue';
import MyInnerComponent from './MyInnerComponent.vue';
export default defineComponent({
name: 'OuterComponent',
props: {
msg: {
type: String,
required: true,
},
},
components: { MyInnerComponent }
});
</script> Here is <template>
<div>
<h2>This is the inner component</h2>
<p>{{ msg }}</p>
</div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'MyInnerComponent',
props: {
msg: {
type: String,
required: true,
},
},
});
</script> |
Beta Was this translation helpful? Give feedback.
-
Just import directly: import { InnerComponent, OuterComponent } from 'my-lib'; |
Beta Was this translation helpful? Give feedback.
-
I need to use
The users of the plugin must not need to use
|
Beta Was this translation helpful? Give feedback.
-
Assuming the scenario that you published this component in some repository and pointed out the main correctly in package.json, follow my install file:
The components file has this content:
I use rollup to build, you can see the whole process in this repo of my OpenSource Project. |
Beta Was this translation helpful? Give feedback.
-
I solved the issue by migrating from That is all: Here is the new {
"name": "vue3-plugin",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
"main": "./dist/vue3-plugin.cjs",
"module": "./dist/vue3-plugin.js",
"exports": {
".": {
"import": "./dist/vue3-plugin.js",
"require": "./dist/vue3-plugin.cjs"
}
},
"dependencies": {
"vue": "^3.3.4"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"typescript": "^5.0.2",
"vite": "^4.4.5",
"vue-tsc": "^1.8.5"
}
} Here is import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
build: {
lib: {
entry: './src/main.ts',
name: 'vue3-plugin',
fileName: 'vue3-plugin',
},
rollupOptions: {
external: ['vue'],
output: {
globals: {
vue: 'Vue',
},
},
},
},
}) Here is import MyOuterComponent from './MyOuterComponent.vue'
import MyInnerComponent from './MyInnerComponent.vue'
export default {
install(Vue: any) {
Vue.component('my-inner-component', MyInnerComponent)
Vue.component('my-outer-component', MyOuterComponent)
},
} |
Beta Was this translation helpful? Give feedback.
I solved the issue by migrating from
vue-cli-service
tovite
withrollup
.That is all:
vue-cli-service -> vite
.Here is the new
package.json
: