vue-render函数的三个参数

 第一个参数(必须) - {String | Object | Function}

Vue.component('elem', {render: function(createElement) {return createElement('div');//一个HTML标签字符/*return createElement({template: '<div></div>'//组件选项对象});*//*var func = function() {return {template: '<div></div>'}};return createElement(func());//一个返回HTML标签字符或组件选项对象的函数*/}});

第二个参数(可选) - {Object}

Vue.component('elem', {render: function(createElement) {var self = this;return createElement('div', {//一个包含模板相关属性的数据对象'class': {foo: true,bar: false},style: {color: 'red',fontSize: '14px'},attrs: {id: 'foo'},domProps: {innerHTML: 'baz'}});}});

第三个参数(可选) - {String | Array}

Vue.component('elem', {render: function(createElement) {var self = this;// return createElement('div', '文本');//使用字符串生成文本节点return createElement('div', {},[//由createElement函数构建而成的数组createElement('h1', '主标'),//createElement函数返回VNode对象createElement('h2', '副标')]);}});

与template写法对比

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><ele></ele></div><script>/*Vue.component('ele', {template: '<div id="elem" :class="{show: show}" @click="handleClick">文本</div>',data: function() {return {show: true}},methods: {handleClick: function() {console.log('clicked!');}}});*/Vue.component('ele', {render: function(createElement) {return createElement('div', {'class': {show: this.show},attrs: {id: 'elem'},on: {click: this.handleClick}}, '文本');},data: function() {return {show: true}},methods: {handleClick: function() {console.log('clicked!');}}});new Vue({el: '#app'});</script>
</body>
</html>

this.$slots用法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><blog-post><h1 slot="header"><span>About Me</span></h1><p>Here's some page content</p><p slot="footer">Copyright 2016 Evan You</p><p>If I have some content down here</p></blog-post></div><script>Vue.component('blog-post', {render: function(createElement) {var header = this.$slots.header,//返回由VNode组成的数组body = this.$slots.default,footer = this.$slots.footer;return createElement('div', [createElement('header', header),createElement('main', body),createElement('footer', footer)])}});new Vue({el: '#app'});</script>
</body>
</html>

使用props传递数据

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><ele :show="show"></ele><ele :show="!show"></ele></div><script>Vue.component('ele', {render: function(createElement) {if (this.show) {return createElement('p', 'true');} else {return createElement('p', 'false');}},props: {show: {type: Boolean,default: false}}});new Vue({el: '#app',data: {show: false}});</script>
</body>
</html>

v-model指令

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><el-input :name="name" @input="val=>name=val"></el-input><div>你的名字是{{name}}</div></div><script>Vue.component('el-input', {render: function(createElement) {var self = this;return createElement('input', {domProps: {value: self.name},on: {input: function(event) {self.$emit('input', event.target.value);}}})},props: {name: String}});new Vue({el: '#app',data: {name: 'hdl'}});</script>
</body>
</html>

作用域插槽

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><ele><template scope="props"><span>{{props.text}}</span></template></ele></div><script>Vue.component('ele', {render: function(createElement) {// 相当于<div><slot :text="msg"></slot></div>return createElement('div', [this.$scopedSlots.default({text: this.msg})]);},data: function() {return {msg: '来自子组件'}}});new Vue({el: '#app'});</script>
</body>
</html>

向子组件中传递作用域插槽

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><ele></ele></div><script>Vue.component('ele', {render: function(createElement) {return createElement('div', [createElement('child', {scopedSlots: {default: function(props) {return [createElement('span', '来自父组件'),createElement('span', props.text)];}}})]);}});Vue.component('child', {render: function(createElement) {return createElement('b', this.$scopedSlots.default({text: '我是组件'}));}});new Vue({el: '#app'});</script>
</body>
</html>

函数化组件

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>render</title><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
</head>
<body><div id="app"><smart-item :data="data"></smart-item><button @click="change('img')">切换为图片为组件</button><button @click="change('video')">切换为视频为组件</button><button @click="change('text')">切换为文本组件</button></div><script>// 图片组件选项var ImgItem = {props: ['data'],render: function(createElement) {return createElement('div', [createElement('p', '图片组件'),createElement('img', {attrs: {src: this.data.url}})]);}}// 视频组件var VideoItem = {props: ['data'],render: function(createElement) {return createElement('div', [createElement('p', '视频组件'),createElement('video', {attrs: {src: this.data.url,controls: 'controls',autoplay: 'autoplay'}})]);}};/*纯文本组件*/var TextItem = {props: ['data'],render: function(createElement) {return createElement('div', [createElement('p', '纯文本组件'),createElement('p', this.data.text)]);}};Vue.component('smart-item', {functional: true,render: function(createElement, context) {function getComponent() {var data = context.props.data;if (data.type === 'img') return ImgItem;if (data.type === 'video') return VideoItem;return TextItem;}return createElement(getComponent(),{props: {data: context.props.data}},context.children)},props: {data: {type: Object,required: true}}});new Vue({el: '#app',data() {return {data: {}}},methods: {change: function(type) {if (type === 'img') {this.data = {type: 'img',url: 'https://raw.githubusercontent.com/iview/iview/master/assets/logo.png'}} else if (type === 'video') {this.data = {type: 'video',url: 'http://vjs.zencdn.net/v/oceans.mp4'}} else if (type === 'text') {this.data = {type: 'text',content: '这是一段纯文本'}}}},created: function() {this.change('img');}});</script>
</body>
</html>

渲染动态表单用法

<template><div><form><div v-for="(field, index) in fields" :key="index"><component :is="field.type" :name="field.name" :label="field.label" :options="field.options" v-model="form[field.name]" /></div></form></div>
</template><script>
import Vue from 'vue';export default {data() {return {form: {},fields: [{ type: 'text', name: 'username', label: '用户名' },{ type: 'password', name: 'password', label: '密码' },{ type: 'select', name: 'gender', label: '性别', options: [{ value: 'male', label: '男' }, { value: 'female', label: '女' }] },{ type: 'checkbox', name: 'hobbies', label: '爱好', options: [{ value: 'reading', label: '阅读' }, { value: 'music', label: '音乐' }, { value: 'sports', label: '运动' }] },{ type: 'radio', name: 'status', label: '状态', options: [{ value: 'active', label: '激活' }, { value: 'inactive', label: '未激活' }] },],};},render(h) {const self = this;const fields = self.fields.map((field) => {const props = {props: {name: field.name,label: field.label,options: field.options,value: self.form[field.name],},on: {input: (value) => {self.form[field.name] = value;},},};return h(field.type, props);});return h('div', fields);},
};
</script><style>
/* 样式省略 */
</style>

在示例代码中,我们定义了一个表单组件,其中的 `fields` 数组包含了表单中的各个字段,每个字段都有一个 `type` 属性表示字段的类型,例如文本框、密码框、下拉框等。在 `render` 函数中,我们使用 `map` 方法遍历 `fields` 数组,根据每个字段的类型动态生成对应的组件,并将组件的属性和事件绑定到表单数据中。最后,我们将所有生成的组件包裹在一个 `div` 元素中返回。

需要注意的是,由于在 `render` 函数中动态生成了组件,因此在模板中无法直接使用这些组件。如果需要在模板中使用这些组件,可以通过在 `render` 函数中将生成的组件保存到一个数组中,然后在模板中使用 `v-for` 渲染这个数组。

希望这个示例能帮助您理解如何使用 Vue 的 render 函数渲染动态表单。如有任何问题,请随时提问。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/131289.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用electron ipcRenderer接收通信消息多次触发

使用electron ipcRenderer接收通信消息多次触发 在使用electron ipcRenderer.on接收ipcRenderer.send的返回值时&#xff0c;ipcRenderer.send发送一次信息&#xff0c; ipcRenderer.on会打印多个日志&#xff0c; renderer.once(get-file-path, (event: any, paths: any) &g…

商用车自动驾驶进入「拐点」时刻

对于自动驾驶的商业化落地来说&#xff0c;这个「性感」的赛道一直备受争议。在过去几年&#xff0c;包括港口、矿山等在内的封闭场景进入商业订单的收获期&#xff1b;但类似干线物流这样的半开放式场景&#xff0c;却喜忧参半。 今年初&#xff0c;作为全球自动驾驶领域的技…

利用MySQL玩转数据分析之基础篇

知识无底&#xff0c;学海无涯&#xff0c;到今天进入MySQL的学习4天了&#xff0c;知识点虽然简单&#xff0c;但是比较多&#xff0c;所以写一篇博客将MySQL的基础写出来&#xff0c;方便自己以后查找&#xff0c;还有就是分享给大家。 1、SQL简述 1&#xff09;SQL的概述 S…

windows 用vs创建cmake工程并编译opencv应用项目生成exe流程简述

目录 前言一、安装opencv&#xff08;1&#xff09;下载&#xff08;2&#xff09;双击安装&#xff08;3&#xff09;环境变量和system文件夹设置 二、打开vs创建项目三、编辑cpp&#xff0c;.h&#xff0c;cmakelist.txt文件&#xff08;1&#xff09;h文件&#xff08;2&…

【Python从入门到进阶】41、有关requests代理的使用

接上篇《40、requests的基本使用》 上一篇我们介绍了requests库的基本使用&#xff0c;本篇我们来学习requests的代理。 一、引言 在网络爬虫和数据抓取的过程中&#xff0c;我们经常需要发送HTTP请求来获取网页内容或与远程服务器进行通信。然而&#xff0c;在某些情况下&…

生成Linux系统下的一些文件

生成Linux系统下的一些文件 文章目录 生成Linux系统下的一些文件1. Initrd1.1 dracut命令1.2 mkinitramfs命令 2. GRUB2.1 Grub2.2 grub.cfg2.3 grub.efi 3. fstab3.1 自动更新3.2 手动更新 4. 生成用户 1. Initrd 通常, lib/modules/下的文件夹名称和内核的版本名是一致的, 所…

通过在Z平面放置零极点的来设计数字滤波器

文章来源地址&#xff1a;https://www.yii666.com/blog/393376.html 通过在Z平面放置零极点的来设计数字滤波器 要求&#xff1a;设计一款高通滤波器&#xff0c;用在音频信号处理过程中&#xff0c;滤掉100Hz以下的信号。 实现方法&#xff1a;通过在Z平面放置零极点的来设…

数据结构与算法【02】—线性表

CSDN系列专栏&#xff1a;数据结构与算法专栏 针对以前写的数据结构与算法系列重写(针对文字描述、图片、错误修复)&#xff0c;改动会比较大&#xff0c;一直到更新完为止 前言 通过前面数据结构与算法基础知识我们知道了数据结构的一些概念和重要性&#xff0c;那么本章总结…

华为政企光传输网络产品集

产品类型产品型号产品说明 maintainProductEA5800-X15 典型配置 上行160G 下行64口GPON 16口XGS PONEA5800系列多业务接入设备定位为面向NG-PON的下一代OLT&#xff0c;基于分布式架构&#xff0c;运用虚拟接入技术&#xff0c;为用户提供宽带、无线、视频回传等多业务统一承…

15、Nuxt.js代理转发解决跨域问题

nuxt.config.js export default {...// Modules: https://go.nuxtjs.dev/config-modulesmodules: ["nuxtjs/axios"],axios: {proxy: true, // 开启代理转发prefix: "/api"},// 代理转发proxy: {/api: {target: "https://mock.mengxuegu.com/mock/654…

【UE 材质】简单的闪闪发光材质

效果 节点 参考视频&#xff1a; https://www.bilibili.com/video/BV1uK411y737/?vd_source36a3e35639c44bb339f59760641390a8

MySQL(8):聚合函数

聚合函数介绍 聚合函数&#xff1a; 对一组数据进行汇总的函数&#xff0c;输入的是一组数据的集合&#xff0c;输出的是单个值。 聚合函数类型&#xff1a;AVG(),SUM(),MAX(),MIN(),COUNT() AVG / SUM 只适用于数值类型的字段&#xff08;或变量&#xff09; SELECT AVG(…

【LeetCode】每日一题 2023_11_4 数组中两个数的最大异或值

文章目录 刷题前唠嗑题目&#xff1a;数组中两个数的最大异或值题目描述代码与解题思路 结语 刷题前唠嗑 LeetCode? 启动&#xff01;&#xff01;&#xff01; 题目&#xff1a;数组中两个数的最大异或值 题目链接&#xff1a;421. 数组中两个数的最大异或值 题目描述 代…

python问题笔记2

70 列表嵌套元组,分别按字母和数字排序 您可以使用Python中的sorted()函数来对列表中的元组进行排序。首先,您需要定义一个自定义的排序函数,以便根据字母或数字进行排序。 以下是一个例子,展示如何按字母和数字分别对嵌套元组进行排序: def sort_by_letter(item):retu…

前端埋点方式

前言&#xff1a; 想要了解用户在系统中所做的操作&#xff0c;从而得出用户在本系统中最常用的模块、在系统中停留的时间。对于了解用户的行为、分析用户的需求有很大的帮助&#xff0c;想实现这种需求可以通过前端埋点的方式。 埋点方式&#xff1a; 1.什么是埋点&#xff1f…

基于Jenkins实现接口自动化持续集成,学完涨薪5k

一、JOB项目配置 1、添加描述 可选选项可填可不填 2、限制项目的运行节点 节点中要有运行环境所需的配置 节点配置教程&#xff1a;https://blog.csdn.net/YZL40514131/article/details/131504280 3、源码管理 需要将脚本推送到远程仓库中 4、构建触发器 可以选择定时构建…

【python】路径管理+路径拼接问题

路径管理 问题相对路径问题绝对路径问题 解决os库pathlib库最终解决 问题 环境&#xff1a;python3.7.16 win10 相对路径问题 因为python的执行特殊性&#xff0c;使用相对路径时&#xff0c;在不同路径下用python指令会有不同的索引效果&#xff08;python的项目根目录根据执…

Temp directory ‘C:\WINDOWS\TEMP‘ does not exist

问题描述 解决方法 管理员权限问题&#xff0c;进入temp文件夹更改访问权限即可。 点击 temp文件夹 属性 -> 安全 -> 高级 -> 更改主体Users权限 给读取和写入权限 参考博客 开发springboot项目时无法启动Temp directory ‘C: \WINDOWS\TEMP‘ does not exist

git本地项目同时推送提交到github和gitee同步

git本地项目同时推送提交到github和gitee同步 同时推送到GitHub和Gitee&#xff08;码云&#xff09;可以通过设置多个远程仓库地址来实现。具体步骤如下&#xff1a; 一、分别推送 # 初始化仓库 git init# 添加远程仓库 git remote add gitee gitgitee.com:bealei/test.git…

C 保留字解释

语句 // 单行注释 /* */ 多行注释 #include 头文件引入声明 #define 预先定义 return 结果返回语句&#xff08;可以带参数&#xff0c;也可不带参数&#xff09; printf(); 输出 if 条件语句 else 条件语句否定分支&#xff08;和 if 连用&a…