解决方案

<template>
  <span ref="latex" class="katex" v-html="rawHTML" />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'Latex',
})
</script>

<script lang="ts" setup>
import katex from 'katex'
import 'katex/dist/katex.css'
import { computed, useSlots } from 'vue'

const rawHTML = computed(() => {
  console.log(useSlots().default?.())
  return katex.renderToString(useSlots().default?.()[0].children, {
    throwOnError: false,
  })
})
</script>

<style scoped></style>

Demo

<latex>\pm\sqrt{a^2 + b^2}</latex>

参考文章

Logo

前往低代码交流专区

更多推荐