首页 > 解决方案 > 如何使用`h`创建一个非空评论节点?

问题描述

以下代码将生成一个空的注释节点,即<!---->.
如何生成非空评论节点,例如<!-- Foo -->使用h

export default {
  render(h) {
    return h(null)
  },
}

标签: javascriptvue.jsvuejs2vuejs3vue-render-function

解决方案


选项 1:h(null)使用字符串作为第二个参数

export default {
  render(h) {
    return h(null, 'This is a comment')  // <!--This is a comment-->
  },
}

选项 2:h(Comment)使用字符串作为第二个参数

import { h, Comment } from 'vue' // Vue 3

export default {
  render() {
    return h(Comment, 'This is a comment')  // <!--This is a comment-->
  },
}

选项 3:createCommentVNode()使用字符串作为参数

import { createCommentVNode } from 'vue' // Vue 3

export default {
  render() {
    return createCommentVNode('This is a comment')  // <!--This is a comment-->
  },
}

推荐阅读