vue.js - 如何正确使用 vue js Web 组件内部的插槽并应用样式
问题描述
我遇到了一个问题,即 Web 组件中的插槽实现未按预期运行。我对 Web 组件、自定义元素和插槽的理解是,插槽中呈现的元素应该从文档而不是Shadow DOM 继承其样式,但是插槽中的元素实际上是被添加到 Shadow DOM 中的,因此忽略了全局样式。我创建了以下示例来说明我遇到的问题。
共享用户界面
这是一个使用 cli ( --target wc --name shared-ui ./src/components/*.vue
)编译成 web 组件的 Vue 应用程序
<template>
<div :class="[$style.collapsableComponent]">
<div :class="[$style.collapsableHeader]" @click="onHeaderClick" :title="title">
<span>{{ title }}</span>
</div>
<div :class="[$style.collapsableBody]" v-if="expanded">
<slot name="body-content"></slot>
</div>
</div>
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component({})
export default class CollapsableComponent extends Vue {
@Prop({ default: "" })
title!: string;
@Prop({default: false})
startExpanded!: boolean;
private expanded: boolean = false;
constructor() {
super();
this.expanded = this.startExpanded;
}
get isVisible(): boolean {
return this.expanded;
}
onHeaderClick(): void {
this.toggle();
}
public toggle(expand?: boolean): void {
if(expand === undefined) {
this.expanded = !this.expanded;
}
else {
this.expanded = expand;
}
this.$emit(this.expanded? 'expand' : 'collapse');
}
public expand() {
this.expanded = true;
}
public collapse() {
this.expanded = false;
}
}
</script>
<style module>
:host {
display: block;
}
.collapsableComponent {
background-color: white;
}
.collapsableHeader {
border: 1px solid grey;
background: grey;
height: 35px;
color: black;
border-radius: 15px 15px 0 0;
text-align: left;
font-weight: bold;
line-height: 35px;
font-size: 0.9rem;
padding-left: 1em;
}
.collapsableBody {
border: 1px solid black;
border-top: 0;
border-radius: 0 0 10px 10px;
padding: 1em;
}
</style>
共享用户界面消费者
这是一个使用标准脚本包含文件导入 shared-ui web 组件的 vue 应用程序。
应用程序.vue<template>
<div id="app">
<shared-ui title="Test">
<span class="testClass" slot="body-content">
Here is some text
</span>
</shared-ui>
</div>
</template>
<script lang="ts">
import 'vue'
import { Component, Vue } from 'vue-property-decorator';
@Component({ })
export default class App extends Vue {
}
</script>
<style lang="scss">
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.testClass{
color: red;
}
</style>
main.ts
import Vue from "vue";
import App from "./App.vue";
Vue.config.productionTip = false;
// I needed to do this so the web component could reference Vue
(window as any).Vue = Vue;
new Vue({
render: h => h(App),
}).$mount('#app');
在此示例中,我希望容器内的内容具有红色文本,但是因为 Vue 将元素克隆到 Shadow DOM 中,所以 .testClass 样式被忽略并且文本以黑色填充呈现。
如何将 .testClass 应用于我的 Web 组件内的元素?
解决方案
好的,所以我设法找到了一种解决方法,它使用本机插槽并将子组件正确呈现在 DOM 中的正确位置。
在挂载的事件中连接下一个刻度线,用新的插槽替换插槽容器的 innerHtml。您可以花哨并为命名插槽做一些很酷的替换等等,但这应该足以说明解决方法。
共享用户界面
这是一个使用 cli ( --target wc --name shared-ui ./src/components/*.vue
)编译成 web 组件的 Vue 应用程序
<template>
<div :class="[$style.collapsableComponent]">
<div :class="[$style.collapsableHeader]" @click="onHeaderClick" :title="title">
<span>{{ title }}</span>
</div>
<div ref="slotContainer" :class="[$style.collapsableBody]" v-if="expanded">
<slot></slot>
</div>
</div>
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component({})
export default class CollapsableComponent extends Vue {
@Prop({ default: "" })
title!: string;
@Prop({default: false})
startExpanded!: boolean;
private expanded: boolean = false;
constructor() {
super();
this.expanded = this.startExpanded;
}
get isVisible(): boolean {
return this.expanded;
}
onHeaderClick(): void {
this.toggle();
}
//This is where the magic is wired up
mounted(): void {
this.$nextTick().then(this.fixSlot.bind(this));
}
// This is where the magic happens
fixSlot(): void {
// remove all the innerHTML that vue has place where the slot should be
this.$refs.slotContainer.innerHTML = '';
// replace it with a new slot, if you are using named slot you can just add attributes to the slot
this.$refs.slotContainer.append(document.createElement('slot'));
}
public toggle(expand?: boolean): void {
if(expand === undefined) {
this.expanded = !this.expanded;
}
else {
this.expanded = expand;
}
this.$emit(this.expanded? 'expand' : 'collapse');
}
public expand() {
this.expanded = true;
}
public collapse() {
this.expanded = false;
}
}
</script>
<style module>
:host {
display: block;
}
.collapsableComponent {
background-color: white;
}
.collapsableHeader {
border: 1px solid grey;
background: grey;
height: 35px;
color: black;
border-radius: 15px 15px 0 0;
text-align: left;
font-weight: bold;
line-height: 35px;
font-size: 0.9rem;
padding-left: 1em;
}
.collapsableBody {
border: 1px solid black;
border-top: 0;
border-radius: 0 0 10px 10px;
padding: 1em;
}
</style>
共享用户界面消费者
这是一个使用标准脚本包含文件导入 shared-ui web 组件的 vue 应用程序。
应用程序.vue<template>
<div id="app">
<shared-ui title="Test">
<span class="testClass" slot="body-content">
Here is some text
</span>
</shared-ui>
</div>
</template>
<script lang="ts">
import 'vue'
import { Component, Vue } from 'vue-property-decorator';
@Component({ })
export default class App extends Vue {
}
</script>
<style lang="scss">
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.testClass{
color: red;
}
</style>
main.ts
import Vue from "vue";
import App from "./App.vue";
Vue.config.productionTip = false;
// I needed to do this so the web component could reference Vue
(window as any).Vue = Vue;
new Vue({
render: h => h(App),
}).$mount('#app');
推荐阅读
- python - Numba 可以用来继承 Keras 层吗?
- javascript - JavaScript 按钮点击事件
- javascript - 是否可以在javascript中将数组转换为NodeList?leetcode问题--链表--
- javascript - 检查数组中的对象是否包含数组中另一个对象中的所有值
- node.js - Twitter OAuth API 超时(Nginx 还是 Twitter?)
- excel-formula - 什么是阿拉伯上标数字?
- javascript - 我可以传递一个对象作为 onClick 函数的参数吗
- php - 如何从 PHP/MySQL 中检索逗号分隔的值?
- amazon-web-services - 如何从 Cognito 用户池获取联合 API 的访问令牌
- mysql - 如何从具有其他条件的表中删除多行?