We are attempting to create a Vue npm package where one component imports another component (a simple example might be - the package contains a generic button component, and a table component that uses my button component for paging). I can import the child component into the parent component and build the package successfully. But when I import and use the parent component (from the package) in an application, the child component content does not display. Skips past it like it isn't there.
I am at a loss.
I am using some very simple components for testing:
<!-- Child Component -->
<template>
<div id="childElement">Child Component</div>
</template>
<script>
export default {
name: "childComponent"
}
</script>
<style>
#childElement {
font-weight: bold;
}
</style>
<!-- Parent Component -->
<template>
<div>
<div id="parentElement">Parent Component</div>
<child-component></child-component>
</div>
</template>
<script>
import ChildComponent from "./ChildComponent.vue";
export default {
name: "parentComponent",
components: {
ChildComponent
}
}
</script>
<style>
#parentElement {
font-weight: bold;
}
</style>
(Edit) Here is my index.js file
import ParentComponent from './components/ParentComponent.vue';
import ChildComponent from './components/ChildComponent.vue';
const install = Vue => {
Vue.component("ParentComponent", ParentComponent);
Vue.component("ChildComponent", ChildComponent);
};
export default {
install
};
export { ParentComponent, ChildComponent };
Usage (in a different app, after a "yarn add ...")
<template>
<div>
<div>
<h1>Testing Section</h1>
<parent-component></parent-component>
<h1>End Testing Section</h1>
</div>
</div>
</template>
<script>
import { ParentComponent, ChildComponent } from ...;
export default {
name: 'TestApp',
components: {
ParentComponent,
ChildComponent
}
};
</script>
And this is what I get on the page:
Testing Section Parent Component End Testing Section
Here are some things that I know:
- The import path in ParentComponent to ChildComponent is correct. If I purposefully change the path to something that doesn't exist, it errors when I try to build.
- If I import and use ChildElement directly in the app, it works as expected.
- If I take ParentComponent and ChildComponent and copy them into the app structure, then import them that way (instead of importing them from the package), then ParentComponent displays ChildComponent exactly the way I would expect it to.
I see other packages out there that seem to import components this way, so I think what I'm doing is possible.
I would be grateful for any thoughts anyone has!
(and let me know if it would be helpful if I included anymore code from the project)
UPDATE I've tracked this down a little more. When I include the child component, I'm getting this warning from the app:
[Vue warn]: resolveComponent can only be used in render() or setup().
which has helped me find other resources on the internets, but nothing has helped. My simple tests aren't explicitly using resolveComponent anywhere. And if I do use it and a render function instead of a template, same result, same warning.
vue.config.js
includeruntimeCompiler: true
. – Gisele