During the development of a Vue web component, I encountered an issue where the CSS styling was not being applied to the web component itself, but instead added to the head of the document. This resulted in the style being ignored within the shadow DOM. To address this, I wrapped the web component in my main.js file like so:
import Vue from 'vue';
import wrap from '@vue/web-component-wrapper';
import MyWebComponent from './components/MyWebComponent';
const WrappedElement = wrap(Vue, MyWebComponent);
window.customElements.define('my-web-component', WrappedElement);
Despite these efforts, the CSS rules within the style tags continued to have no effect.
Interestingly, when I built the application for production, the styles were properly added to the web component. The command I used for wrapping during production build was:
vue-cli-service build --target wc --name my-web-component ./src/components/MyWebComponent.vue
I am now wondering if there is a way to achieve the same result using vue-cli-service serve
.
For reference, here is a link to an example repository: https://github.com/snirp/vue-web-component
After some investigation, I suspect that my problem may be related to this particular issue. While the provided workarounds are confusing to me, I would greatly appreciate a more straightforward solution.