【问题标题】:Use Vue.js 3 fragments with render function使用带有渲染功能的 Vue.js 3 片段
【发布时间】:2021-02-07 03:59:10
【问题描述】:

我应该如何使用带有渲染功能的 Vue 3 片段?下面的代码不应该工作吗?

import { h } from 'vue'

render () {
  return [
    h('label', { htmlFor: 'username' }, this.label),
    h('input', { id: 'username' }),
  ]
},

【问题讨论】:

    标签: javascript vue.js vue-component vuejs3


    【解决方案1】:

    是的,在渲染函数中定义片段的语法是正确的:

    import { h } from "vue";
    export default {
      props: ["label", "errors"],
    
      render() {
        return [
          h("label", { htmlFor: "username" }, this.label),
          h("input", { id: "username" }),
          this.errors && h("span", { class: "red" }, this.errors)
        ];
      }
    };
    
    

    这相当于:

    <template>
     <label for="username"> {{this.label}}</label>
      <input id="username" />
       <span class="red" v-if="errors">{{errors}}</span>
    </template>
    

    LIVE DEMO

    【讨论】:

      猜你喜欢
      • 2022-12-29
      • 2021-01-19
      • 2021-09-10
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 2021-03-30
      相关资源
      最近更新 更多