Translation in progress
This page hasn't been translated into English yet, so the content below is in Chinese. The code examples work the same way.
Divider 分割线
分隔段落或一行里的几项内容。
引入
ts
import { NsDivider } from '@ns/ui'
基础用法
默认是横向的 1px 细线,颜色是主题的边框色,上下各留 16px。
vue
<template>
<p>第一段内容</p>
<NsDivider />
<p>第二段内容</p>
</template>
带文字
默认插槽里的内容显示在线的中间。content-position 可选 left、center(默认)、right,靠一侧时那一侧留 16px 的短线。
vue
<template>
<NsDivider content-position="left">靠左(left)</NsDivider>
<NsDivider>居中(center)</NsDivider>
<NsDivider content-position="right">靠右(right)</NsDivider>
</template>
插槽里可以放图标、标签这类元素,不只是文字:
vue
<template>
<NsDivider><NsIcon name="star" /></NsDivider>
<NsDivider content-position="left">
<NsSpace size="small">
<NsIcon name="calendar" />
<span>2026 年 11 月</span>
</NsSpace>
</NsDivider>
<NsDivider content-position="right"><NsTag type="success" size="small">已完成</NsTag></NsDivider>
</template>
线型
border-style 可选 solid(默认)、dashed、dotted,带不带文字、文字在哪个位置都可以用。
vue
<template>
<NsDivider>实线(solid)</NsDivider>
<NsDivider border-style="dashed">虚线(dashed)</NsDivider>
<NsDivider border-style="dotted">点线(dotted)</NsDivider>
<NsDivider border-style="dashed" />
<NsDivider border-style="dotted" />
<NsDivider border-style="dashed" content-position="left">虚线靠左</NsDivider>
<NsDivider border-style="dotted" content-position="right">点线靠右</NsDivider>
</template>
竖向
direction="vertical" 是竖线,用来分隔一行里的几项,左右各留 8px。
vue
<template>
<p style="margin-bottom: 12px">
编辑<NsDivider direction="vertical" />复制<NsDivider direction="vertical" />删除
</p>
<div style="display: flex; align-items: center; height: 40px">
<span>工具栏</span>
<NsDivider direction="vertical" />
<span>拉伸到行高</span>
</div>
</template>
竖线同样可以用三种线型:
vue
<script setup lang="ts">
import { NsDivider, NsSpace } from '@ns/ui'
import type { DividerBorderStyle } from '@ns/ui'
const borderStyles: DividerBorderStyle[] = ['solid', 'dashed', 'dotted']
</script>
<template>
<NsSpace direction="vertical" align="start">
<div v-for="item in borderStyles" :key="item">
<span class="row-label">{{ item }}</span>
<span>编辑</span>
<NsDivider direction="vertical" :border-style="item" />
<span>复制</span>
<NsDivider direction="vertical" :border-style="item" />
<span>删除</span>
</div>
</NsSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--ns-text-color-secondary);
font-size: var(--ns-font-size-sm);
}
</style>
竖线里也可以放文字,文字上下各留 12px,上下两段线平分剩下的高度。放在有高度的 flex 容器里时,竖线拉伸到容器的高度:
vue
<template>
<div class="login">
<div class="login__item">账号密码登录</div>
<NsDivider direction="vertical">或</NsDivider>
<div class="login__item">扫码登录</div>
</div>
</template>
<style scoped>
.login {
display: flex;
height: 160px;
padding: var(--ns-spacing-4) 0;
box-sizing: border-box;
}
.login__item {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
}
</style>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| direction | 'horizontal' | 'vertical' | 'horizontal' | 方向 |
| contentPosition | 'left' | 'center' | 'right' | 'center' | 文字位置 |
| borderStyle | 'solid' | 'dashed' | 'dotted' | 'solid' | 线型 |
Slots
| 插槽 | 说明 |
|---|---|
| default | 显示在线中间的文字或内容 |
类型
ts
import type { DividerProps } from '@ns/ui'
注意事项
- 竖向分割线至少一个字高(
1em);放在 flex 行里时,即使父元素是align-items: center,也会拉伸到这一行的高度。 - 线的颜色是
--ns-border-color,粗细是--ns-border-width,改主题变量即可统一调整。 - 根元素带
role="separator"和aria-orientation。