Modal组件是iView库中较经常使用的一个,而如何利用render实现一个自定义的Modal在官方wiki中并无详细说明。我在这里将举例说明:前端
所谓自定义内容,是指使用一个自定义的组件做为Modal框的内容。在个人实例中,我使用的内容组件包含有两个Input组件,用于实现两个变量的输入。vue
自定义组件add.vuegit
<template> <Row> <div class="wrapper"> <h4> <Icon type="heart"></Icon> save </h4> </div> <div class="wrapper"> <Input v-model="value1" @on-change="value1Change"></Input> </div> <div class="wrapper"> <Input v-model="value2" @on-change="value2Change"></Input> </div> </Row> </template> <script> export default { name: 'add', data() { return { value1:'', value2:'' } }, methods:{ value1Change:function() { var obj = this this.$emit('value1', obj.value1) }, value2Change:function() { var obj = this this.$emit('value2', obj.value2) } } } </script>
在这个组件中,为两个Input组件分别定义on-change事件的方法。只要value1的值或者value2的值发生变化,就会向父组件emit对应的事件value1和value2。程序员
父组件content.vuegithub
<template> <Button @click="openModal">弹出模态框</Button> </template> <script> import add from './content/add.vue' export default { data() { return { v1:'', v2:'' } }, components:{ add }, methods:{ openModal: function() { this.$Modal.confirm({ scrollable:true, okText:'保存', render: (h) => { return h(add, { props: { }, on: { value1: (value1) => { this.v1 = value1 }, value2: (value2) => { this.v2 = value2 } } }) }, onOk: () => { if (this.v1 == '' || this.v2 == '') { this.$Message.error('信息填写不完整!') } const msg = this.$Message.loading({ content: '正在保存..', duration: 0 }) this.saveLink(msg) } }) } } } </script>
在父组件中,引入自定义内容组件add(注意:箭头函数中组件为 add 而不是 'add' ),并监听事件value1和value2,及时为data中v1和v2赋值。若在此过程当中父组件有须要传递给内容组件的参数,须要在props中填入。后端
onOk项后定义触发模态框中ok按钮的回调函数。本例中是检查input中输入的值是否为空,并在接口中上传数据。app
这样,iView中一个自定义Modal就经过render方法实现了。函数
最近作了个项目,采用先后分离的模式,而前端使用了iView。做为一个后端程序员,碰见了很多坑,项目上线后我会把我解决的问题总结下,陆续写成文章。既方便本身查看,也但愿能够帮助到跟我同样的"小白"。this
我的新项目上线后,我会push到个人github。但愿你们能够体验下,给我点建议,若是能给个star最好不过了。code