8

基于vue的插槽式动态布局组件slot-layout

 4 years ago
source link: https://segmentfault.com/a/1190000040633521
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

因为经常需要做一些动态布局,做了个vue动态布局组件slot-layout,分享下。

这个组件基于一个布局配置对象,通过调整布局配置对象,最终通过映射到对应的vue组件插槽,来动态进行页面布局,布局和配置比较方便。

  • 通过js布局对象,完全控制页面布局。
  • 特别适合用于低代码平台系统,只需在运行时按照需要动态传入一个布局配置对象,即可随意调整页面布局。无需预先编写布局代码。
  • 支持多层布局嵌套,可实现任意数目、任意层次的布局。
  • 支持任意长度单位,如px、vw、%、rem。
  • 支持填充式布局。即区块填充满父容器。
  • 组件自带布局配置功能,可轻松实现布局设计。
    image.png

用法举例如下:

1、简单例子(单层、两个子节点):
查看效果:https://codepen.io/hzsrc/pen/...

image.png

<template>
  <slot-layout class="full" :layout="layout">
    <div slot="left" class="blk f-center">
      left
    </div>
    <div slot="right" class="blk f-center">
      <button @click="layout.isVertical=!layout.isVertical">动态调整布局</button>
    </div>
  </slot-layout>
</template>
<script>
import slotLayout from "https://cdn.skypack.dev/[email protected]";
export default {
  components:{
    slotLayout
  },
  data(){
    return {
      //布局配置对象
      layout: {
        aySize: '100%',
        isVertical: false,
        children: [
            {slot: 'left', laySize: '60px'},
            {slot: 'right', laySize: 'fill'}
        ]
      }
    }
  }
};
</script>

2、复杂的例子:
查看效果

image.png

<template>
    <div class="full">
        <layout-designer v-if="isDesign" class="full" :layout="layout" show-result></layout-designer>
        <slot-layout v-else class="full pd-10" :layout="layout" gulp="5px">
            <aside slot="tree" class="full f-center">aside</aside>
            <header slot="top" class="full f-center">header</header>
            <nav slot="title" class="full f-center">
                <button @click="resetLayout">动态调整布局</button>
            </nav>
            <div slot="right" class="full f-center">right</div>
            <div slot="bottom" class="full f-center">bottom</div>
        </slot-layout>
        <a class="absolute" @click="isDesign=!isDesign">{{isDesign ? '运行布局' : '设计布局'}}</a>
    </div>
</template>

<script>
    import slotLayout from 'slot-Layout'
    import LayoutDesigner from 'slot-Layout/src/design/layoutDesigner';

    export default {
        name: 'slotLay',
        data() {
            return {
                isDesign: true,
                layout: {
                    isVertical: false,
                    laySize: '100%',
                    children: [
                        {
                            laySize: '15vw',
                            slot: 'tree',
                        },
                        {
                            laySize: 'fill',
                            isVertical: true,
                            children: [
                                {
                                    slot: 'title',
                                    laySize: '60px'
                                },
                                {
                                    slot: 'top',
                                    laySize: '30%',
                                },
                                {
                                    slot: 'bottom',
                                    laySize: 'fill',
                                }
                            ]
                        },
                        {
                            laySize: '100px',
                            slot: 'right',
                        },
                    ]
                }
            }
        },
        methods: {
            resetLayout() {
                this.layout.children.reverse()
            }
        },
        components: {
            LayoutDesigner,
            slotLayout
        }
    }
</script>

Recommend

  • 50

    在过去几年中,CSS布局已经发生了巨大的变化,现在我们开发网站的方式也发生了巨大变化。现在我们有需要可选的CSS布局方式来开发我们的网站,这也就要求我们对这些方式能作出正确而双合适的选择。在这篇文章中,将会介绍各种CSS布局的基本使用方式以及使用的目的。...

  • 60
    • www.tuicool.com 6 years ago
    • Cache

    Slot-based UI development in Swift

    One of the most challenging decisions that all programmers have to make on an ongoing basis is when to generalize a solution versus just keeping it tied to a specific use case. It’s equally easy — and potentially damaging...

  • 11

    自定义 View 2-3 定制 Layout 的内部布局 这期是 HenCoder 布局部分的最后一期:重写 onMeasure() 和 onLayout() 来定制 Layout 的内部布局。 这期虽然距离上期的时间比较久,但主要是我的个人原因,...

  • 15

    基于Element组件下动态生成多级表头以及数据 ...

  • 9

    基于nodejs的动态编译 weex 组件的平台 大纲 在苏宁业务快速发展的背景下,以前的大促促销页使用到的组件已经无法满足业务的需求,为此我们设计了一套基于weex的组件动态构建方案,实现了模块的动态编译,减少了不必要的...

  • 4
    • www.zhangxinxu.com 3 years ago
    • Cache

    HTML slot 插槽元素深入

    HTML slot 插槽元素深入 这篇文章发布于 2021年09月24日,星期五,00:04,归类于 HTML相关。 阅读 3300 次, 今日 113 次...

  • 7

    在基于Vue的工作流项目模块中,我们在查看表单明细的时候,需要包含公用表单信息,特定表单信息两部分内容。前者表单数据可以统一呈现,而后者则是不同业务的表单数据不同。为了实现更好的维护性,把它们分开作为两部分处理,但是页面入口设计为统一的呈现页面...

  • 5

    Bootstrap Blazor 模板使用(一)Layout 组件 原文链接:

  • 2

    插槽 slot 就是子组件中提供给父组件使用的一个占位符,用 <slot></slot> 表示,父组件可以给这个占位符内填充任何模板代码,填充的内容会自动替换 <slot></slot&...

  • 12
    • www.fly63.com 3 years ago
    • Cache

    Vue3中插槽(slot)用法汇总

    vue中的插槽相信使用过Vue的小伙伴或多或少的都用过,但是你是否了解它全部用法呢?本篇文章就为大家带来Vue3中插槽的全部用法来帮助大...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK