The problem of Vue+tsx using slot is not replaced

The problem of Vue+tsx using slot is not replaced

Preface

I am planning to write a UI component recently and I want to have a deeper understanding of vue 2.x and 3.x

When building the architecture, prepare to use tsx to write all components

But I encountered a problem using slot in tsx

Find the problem

First, I wrote a basic card component:

card.tsx:

import Component from 'vue-class-component'
import VanUIComponent from '@packs/common/VanUIComponent'
import { VNode } from 'vue'
import { Prop } from 'vue-property-decorator'
import { CardShadowEnum } from '@packs/config/card'

@Component
export default class Card extends VanUIComponent {
  @Prop({
    type: String,
    default: undefined
  }) public headerPadding !: string | undefined

  @Prop({
    type: String,
    default: ''
  }) public title !: string

  @Prop({
    type: String,
    default: CardShadowEnum.Hover
  }) public shadow !: CardShadowEnum

  public static componentName = 'v-card'

  public get wrapperClassName(): string {
    const list: string[] = ['v-card__wrapper']

    list.push(`shadow-${ this.shadow }`)

    return list.join(' ')
  }

  public render(): VNode {
    return (
      <div class={ this.wrapperClassName }>
        <div class="v-card__header" style={ { padding: this.headerPadding } }>
          {
            this.$slots.title ? <slot name="title" /> : <div>{ this.title }</div>
          }
        </div>
        <div class="v-card__body">
          <slot name="default" />
        </div>
        <div class="v-card__footer"></div>
      </div>
    )
  }
}

When using this v-card in examples :

<template>
  <v-card>
    <template #title>1111</template>
  </v-card>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class Components extends Vue {

}
</script>

<style lang="scss" scoped>
.components__wrapper {
  padding: 20px;
}
</style>

I found that after rendering, the browser does not replace slot tag:

No slot tag replacement

I searched Baidu and Google for a day but couldn't find an explanation. After reading the official documentation carefully, there was no similar hint. The documentation of the jsx compiler also didn't state it clearly, except for stating how to use named slot .

solve

The next day, I was still struggling with this and looked up how to write in UI component libraries of element-ui and ant-design-vue , but I couldn't find the corresponding way to use slot using jsx .

Unwilling to give up, I changed the search text and finally found a solution. I changed the code to:

...
  public render(): VNode {
    return (
      <div class={ this.wrapperClassName }>
        <div class="v-card__header" style={ { padding: this.headerPadding } }>
          {
            this.$slots.title ?? <div>{ this.title }</div>
          }
        </div>
        <div class="v-card__body">
          <slot name="default" />
        </div>
        <div class="v-card__footer"></div>
      </div>
    )
  }
...

Check out the browser rendering again:

insert image description here

Problem Solving

postscript

When using jsx / tsx , if the js syntax itself can be solved, or the method itself is registered on this , js is preferred to do it. For example, v-if / v-else can be replaced by雙目運算符. There is really no easy way to do this, so use vue 's directives, such as v-show .

This is the end of this article about the problem of Vue+tsx using slot not being replaced. For more relevant Vue+tsx slot not being replaced, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • About VUE's compilation scope and slot scope slot issues
  • Detailed analysis of the usage and application scenarios of slots in Vue
  • In-depth understanding of slot-scope in Vue (suitable for beginners)
  • Solve the problem of secondary encapsulation and slots rendering of ant design vue table a-table
  • Vue slot_Special features slot, slot-scope and directive v-slot description
  • Detailed explanation of the usage of scoped slots in Vue.js slots

<<:  Linux firewall status check method example

>>:  Detailed explanation of Linux less command examples

Recommend

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

Font references and transition effects outside the system

Copy code The code is as follows: <span style=...

Detailed explanation of EXT series file system formats in Linux

Linux File System Common hard disks are shown in ...

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

Detailed description of component-based front-end development process

Background <br />Students who work on the fr...

MySQL database master-slave configuration tutorial under Windows

The detailed process of configuring the MySQL dat...

A simple example of using Vue3 routing VueRouter4

routing vue-router4 keeps most of the API unchang...

Sample code for installing ElasticSearch and Kibana under Docker

1. Introduction Elasticsearch is very popular now...

Vue imports Echarts to realize line scatter chart

This article shares the specific code of Vue impo...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

centos 7 modify sshd | prohibit root login and sshd port script definition

1. Create a new user wwweee000 [root@localhost ~]...