Vue.js渲染函数相关问题:自定义模板转换与外部模板接收
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
refgoes into thereffield of the data object :prop="value"becomesprops: { prop: value }v-modelsplits into avalueprop and aninputevent listener- Child elements are passed as the third argument to
h(), in an array
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

