vue3中再<script setup>标签下使用渲染函数render方法

2023-10-23 15:33:40

有时候未了方便操作组件,我们需要时用渲染函数来编写组件,再vue3可以再setup函数中返回内容来编写组件,但有时候我们直接使用了<script setup>标签应该如何写呢

第一种方式就是普通的渲染函数来编写 (推荐)

<script setup lang="ts">import { ref,h } from 'vue'const props = defineProps<{    text: string
}>()const handleClick = () => {    console.log('click!!')
}const root=h('button',
             {type:'button',onClick:handleClick,class:'btn btn-primary'},props.text)</script><template>
  <root/>             </template>

其实把root函数写成render函数更直观


第二种方式是在tsx模式下,这个模式需要另外配置vue工程,不推荐使用

<script lang="tsx" setup>   import { h } from 'vue';   const render = () => {     return h('div', []);   };   const jsxNode = () => {     return <div> text </div>;   }; </script> <template>   <render />   <jsxNode /> </template>

在渲染函数模式下,我们就可以更改父组件传过来的slots内容了,以达到先修改后生效的效果。

下面以修改slots中的class为例


slots = ()
animationClass = =()=> {
  slotValue = slots.?.()
  (slotValue) {
    ([] && [].&& []..class){
      []..class =[]..class .(animationClass)[]..class =[]..class + +animationClass}
  }
  slotValue[]}


  • 2021-01-15 13:06:08

    监控 MongoDB -

    随着MongoDB中保存的数据越来越多,对MongoDB服务状态的监控也越来越重要,经常关注服务是否健康,才能防止故障以及优化。

  • 2021-01-15 13:30:21

    MongoDb web 用户界面

    MongoDB 的 Web 界面访问端口比服务的端口多1000。 如果你的MongoDB运行端口使用默认的27017,你可以在端口号为28017访问web用户界面,即地址为:http://localhost:28017。

  • 2021-01-16 09:31:14

    Xcode两种引入图片的方法

    如果是纯代码,图片名需要手动添加@2x,@3x等倍数标识,且需要指明后缀.png,.jpg;IB添加图片的话只需要指明后缀就好,不用添加倍数标识。

  • 2021-01-16 09:39:32

    iOS 更改状态栏、导航栏颜色,电池颜色

    注意事项,两种方法设置View controller-based status bar appearance 的值不一样,并且如果你的plist里面没有View controller-based status bar appearance,你需要新建一个。然后就可以成功了。