You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue.js渲染函数相关问题:自定义模板转换与外部模板接收

Q1: How to Convert a Custom Template to Vue.js Render Function?

Converting a template to a render function boils down to translating each element, directive, and binding into calls to Vue's createElement (usually aliased as h). Let's use your provided Form template snippet to walk through this:

Original Template Snippet

<Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
  <FormItem label="Name" prop="name">
    <Input v-model="formValidate.name" />
  </FormItem>
</Form>

Equivalent Render Function

Here's how this translates to a render function (integrated into your modal example):

var custom_form_modal = function (context, custom_form) {
  context.$Modal.info({
    render: (h) => {
      return h('Form', {
        // Map template attributes to the render function's data object
        ref: 'formValidate',
        props: {
          model: context.formValidate,
          rules: context.ruleValidate,
          labelWidth: 80
        }
      }, [
        // Pass children as an array of h() calls
        h('FormItem', {
          props: {
            label: 'Name',
            prop: 'name'
          }
        }, [
          // Handle v-model manually (it's sugar for value + input event)
          h('Input', {
            props: {
              value: context.formValidate.name
            },
            on: {
              input: (val) => { context.formValidate.name = val }
            }
          })
        ])
      ]);
    }
  });
};

Key mapping rules to remember:

  • Template ref goes into the ref field of the data object
  • :prop="value" becomes props: { prop: value }
  • v-model splits into a value prop and an input event listener
  • Child elements are passed as the third argument to h(), in an array
Q2: Can Vue's Render Function Receive External Partial Templates?

Absolutely! There are a few straightforward ways to integrate external template content into your render function, depending on how your custom_form is provided:

1. If custom_form is a Pre-Compiled Render Function

If your external content is already a render function (e.g., from a pre-compiled component), you can call it directly inside your modal's render function:

var custom_form_modal = function (context, custom_form_render) {
  context.$Modal.info({
    render: (h) => {
      // Execute the external render function with the current h and context
      return custom_form_render.call(context, h);
    }
  });
};

2. If custom_form is a Template String

Use Vue's compile method (available in the full Vue build, not runtime-only) to turn the template string into a render function on the fly:

var custom_form_modal = function (context, custom_form_template) {
  context.$Modal.info({
    render: (h) => {
      // Compile the template string into a render function
      const { render } = Vue.compile(custom_form_template);
      // Execute the compiled render function with the correct context
      return render.call(context, h);
    }
  });
};

Note: If you're using the runtime-only Vue build (common in CLI setups), pre-compile the template into a component beforehand instead of using Vue.compile at runtime.

3. If custom_form is a Vue Component

Render it directly using h() like any other component:

var custom_form_modal = function (context, CustomFormComponent) {
  context.$Modal.info({
    render: (h) => {
      return h(CustomFormComponent, {
        // Pass props to the external component if needed
        props: {
          model: context.formValidate,
          rules: context.ruleValidate
        }
      });
    }
  });
};

内容的提问来源于stack exchange,提问作者aircraft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:11:47