vue props传入render函数,实现动态组件嵌套

本文基础是已经了解render函数的使用方式,如果您还不了解render是什么,请阅读vue官方-渲染函数 & JSX

任何一个功能都离不开需求,所以先上图,公司要求做这样一个页面

在这里插入图片描述

从图里可以看出来其实外边框是一样的,只不过位置跟大小还有里面的内容是不一样的,所以想到能不能把外边的边框单独抽出来,里面单独渲染
封装的组件代码如下:

<template>
  <div :style="boxSizeStyle">
    <div class="dataAll maginS">
      <div class="dataAllBorder01">
        <div class="dataAllBorder02">
          <div class="data_tit1">{{setting.title}}</div>
          <container v-if="this.render" class="data_container"></container>
          <div v-else></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  export default {
    name: "box",
    props: {
      setting: {
        type: Object,
        required: true,
        default: {
          title: ''
        }
      },
      render: {
        type: Function
      }
    },
    computed: {
      boxSizeStyle() {
        return {
          height: this.setting.height || '100%',
          width: this.setting.width || '100%'
        };
      }
    },
    data() {
      return {

      }
    },
    beforeMount() {
      if (this.render) {
        Vue.component('container', {
          render: this.render
        })
      }
    },
    mounted() {

    }
  }
</script>

<style scoped>
  .dataAll {
    width: 100%;
    height: 100%;
    float: left;
    border-radius: 10px;
  }

  .dataAllBorder01 {
    width: 100%;
    height: 100%;
    border-radius: 10px;
    border: 1px #0174f5 solid;
    padding: 1px;
    box-sizing: border-box;
  }

  .dataAllBorder02 {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border: 2px solid #016ae0;
    border-radius: 10px;
  }

  .data_tit1 {
    width: 46%;
    height: 28px;
    background-image: url(../../assets/tit01s.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin: 0 auto;
    font-size: 12px;
    line-height: 26px;
    color: #ffffff;
    text-align: center;
  }

  .data_container {
    margin: 3px 8px 8px 8px
  }
</style>

tit01s.png

tit01s.png

外部调用(项目是用的iview写的,用到了Row跟Col组件)

<template>
  <div>
    <Row class="all-row">
      <Col span="6" class="all-col">
        <box v-for="(setting, index) in leftSettings" :key="index" :setting="setting" :render="setting.render">
        </box>
      </Col>
      <Col span="12" class="all-col">
        <box :setting="centerSetting" :render="centerSetting.render">
        </box>
      </Col>
      <Col span="6" class="all-col">
        <box v-for="(setting, index) in rightSettings" :key="index" :setting="setting" :render="setting.render">
        </box>
      </Col>
    </Row>

  </div>

</template>

<script>
  import Box from '../_comps/box'
  import Test from './test'
  export default {
    name: "story",
    components: {
      Box, Test
    },
    data() {
      return {
        leftSettings: [
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
        ],
        rightSettings: [
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
          {
            title: 'xxx分析',
            height: '33.33%',
            width: '100%',
            render: h => h(Test)
          },
        ],
        centerSetting: {
          title: 'xxx分析',
          height: '100%',
          width: '100%',
          render: h => h(Test)
        }
      }
    }
  }
</script>

<style scoped>
  .all-row {
    height: 100vh;
  }
  .all-col {
    height: 100%;
  }
</style>

下载链接(免费):https://download.csdn.net/download/qq_35134375/12969274

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,我们可以使用props属性来向子组件递数据。在使用render函数渲染动态组件时,也可以通过props递数据。 首先,在父组件中定义一个名为"component"的props属性,用于接收要渲染的子组件的名称。然后,在父组件render函数中,使用动态组件的方式来渲染子组件,将props属性递给子组件。 具体步骤如下: 1. 在父组件中,定义一个名为"component"的props属性,并将要渲染的子组件的名称作为其默认值。 ```javascript props: { component: { type: String, default: 'ChildComponent1' // 子组件的名称 } } ``` 2. 在父组件render函数中,使用Vue的createElement函数来渲染动态组件。将子组件的名称通过props递给子组件。 ```javascript render(h) { return h(this.component, { props: { // 将props递给子组件 } }) } ``` 这样,父组件就可以通过修改"component"属性的值来动态渲染不同的子组件了。子组件会接收到父组件递的信息,并根据props的值来进行相应的操作。 例如,如果想要渲染"ChildComponent1"和"ChildComponent2"两个子组件,可以通过改变父组件的"component"属性的值来实现: ```javascript <template> <div> <button @click="changeComponent('ChildComponent1')">Child Component 1</button> <button @click="changeComponent('ChildComponent2')">Child Component 2</button> <component :component="component"></component> </div> </template> <script> export default { data() { return { component: 'ChildComponent1' } }, methods: { changeComponent(componentName) { this.component = componentName } } } </script> ``` 以上就是使用props递给render函数实现动态组件嵌套的方法。通过props递,可以灵活地控制渲染的子组件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值