Skip to content

🎨 组件库使用指南

Lyt.js 提供了一个包含 38+ 个精心设计的组件,覆盖常见的 UI 需求。


📦 安装

bash
npm install @lytjs/components

🚀 快速开始

方式 1:批量注册所有组件

typescript
import { createApp } from '@lytjs/core'
import LytComponents from '@lytjs/components'

const app = createApp({
  template: `
    <div>
      <lyt-button type="primary">点击我</lyt-button>
      <lyt-input v-model="value" placeholder="请输入" />
    </div>
  `,
  state: { value: '' }
})

// 批量注册所有组件
app.use(LytComponents)

app.mount('#app')

方式 2:按需注册组件

typescript
import { createApp } from '@lytjs/core'
import { Button, Input, Avatar } from '@lytjs/components'

const app = createApp({
  // ...
})

// 按需注册组件
app.component('LytButton', Button)
app.component('LytInput', Input)
app.component('LytAvatar', Avatar)

app.mount('#app')

🎯 组件分类

基础组件 (5个)

组件描述导入名
Button按钮组件Button
Icon图标组件Icon
Link链接组件Link
Container容器组件Container
Divider分割线组件Divider

表单组件 (9个)

组件描述导入名
Input输入框Input
Checkbox复选框Checkbox
Radio单选框Radio
Select选择器Select
Switch开关Switch
Form表单容器Form
DatePicker日期选择器DatePicker
TimePicker时间选择器TimePicker
Calendar日历Calendar

反馈组件 (4个)

组件描述导入名
Modal模态框Modal
Toast轻提示Toast
Alert警告提示Alert
Tooltip文字提示Tooltip

导航组件 (4个)

组件描述导入名
Tabs标签页Tabs
Breadcrumb面包屑Breadcrumb
Pagination分页Pagination
Carousel轮播图Carousel

数据展示组件 (6个)

组件描述导入名
Table表格Table
Tag标签Tag
Badge徽章Badge
Spin加载Spin
Empty空状态Empty
Avatar头像Avatar

扩展组件 (10个)

组件描述导入名
DataTable数据表格DataTable
Dialog对话框Dialog
Notification通知Notification
Popover气泡卡片Popover
TabNav标签导航TabNav
Collapse折叠面板Collapse
Dropdown下拉菜单Dropdown
Progress进度条Progress
Slider滑块Slider
Upload上传Upload
Tree树形控件Tree

📝 常用组件示例

Button 按钮

vue
<template>
  <div>
    <!-- 基础用法 -->
    <lyt-button>默认按钮</lyt-button>
    
    <!-- 不同类型 -->
    <lyt-button type="primary">主要按钮</lyt-button>
    <lyt-button type="success">成功按钮</lyt-button>
    <lyt-button type="warning">警告按钮</lyt-button>
    <lyt-button type="danger">危险按钮</lyt-button>
    
    <!-- 不同大小 -->
    <lyt-button size="small">小按钮</lyt-button>
    <lyt-button size="large">大按钮</lyt-button>
    
    <!-- 禁用状态 -->
    <lyt-button disabled>禁用按钮</lyt-button>
    
    <!-- 加载状态 -->
    <lyt-button loading>加载中</lyt-button>
    
    <!-- 图标按钮 -->
    <lyt-button icon="search">搜索</lyt-button>
  </div>
</template>

Input 输入框

vue
<template>
  <div>
    <!-- 基础用法 -->
    <lyt-input v-model="value" placeholder="请输入" />
    
    <!-- 不同类型 -->
    <lyt-input v-model="value" type="password" placeholder="密码" />
    <lyt-input v-model="value" type="number" placeholder="数字" />
    
    <!-- 禁用状态 -->
    <lyt-input v-model="value" disabled placeholder="禁用" />
    
    <!-- 带前缀/后缀 -->
    <lyt-input v-model="value" prefix="https://" suffix=".com" />
    
    <!-- 带图标 -->
    <lyt-input v-model="value" prefix-icon="search" placeholder="搜索" />
  </div>
</template>

<script>
export default {
  data() {
    return { value: '' }
  }
}
</script>

Avatar 头像

vue
<template>
  <div>
    <!-- 基础用法 -->
    <lyt-avatar>U</lyt-avatar>
    <lyt-avatar>用户</lyt-avatar>
    
    <!-- 不同大小 -->
    <lyt-avatar size="small">U</lyt-avatar>
    <lyt-avatar size="large">用户</lyt-avatar>
    
    <!-- 图片头像 -->
    <lyt-avatar src="https://example.com/avatar.jpg" />
    
    <!-- 不同形状 -->
    <lyt-avatar shape="square">U</lyt-avatar>
    <lyt-avatar shape="circle">用户</lyt-avatar>
  </div>
</template>
vue
<template>
  <div>
    <lyt-carousel :autoplay="true" :interval="3000">
      <lyt-carousel-item>
        <div style="background: #99a9bf; text-align: center; line-height: 300px; color: #fff; font-size: 24px;">
          幻灯片 1
        </div>
      </lyt-carousel-item>
      <lyt-carousel-item>
        <div style="background: #d3dce6; text-align: center; line-height: 300px; color: #fff; font-size: 24px;">
          幻灯片 2
        </div>
      </lyt-carousel-item>
      <lyt-carousel-item>
        <div style="background: #99a9bf; text-align: center; line-height: 300px; color: #fff; font-size: 24px;">
          幻灯片 3
        </div>
      </lyt-carousel-item>
    </lyt-carousel>
  </div>
</template>

DatePicker 日期选择器

vue
<template>
  <div>
    <lyt-date-picker v-model="date" placeholder="选择日期" />
  </div>
</template>

<script>
export default {
  data() {
    return { date: '' }
  }
}
</script>

TimePicker 时间选择器

vue
<template>
  <div>
    <lyt-time-picker v-model="time" placeholder="选择时间" />
  </div>
</template>

<script>
export default {
  data() {
    return { time: '' }
  }
}
</script>

Calendar 日历

vue
<template>
  <div>
    <lyt-calendar v-model="selectedDate" />
  </div>
</template>

<script>
export default {
  data() {
    return { selectedDate: new Date() }
  }
}
</script>
vue
<template>
  <div>
    <lyt-button @click="visible = true">打开模态框</lyt-button>
    
    <lyt-modal 
      v-model="visible" 
      title="提示"
      @confirm="handleConfirm"
      @cancel="handleCancel"
    >
      <p>这是一段内容</p>
    </lyt-modal>
  </div>
</template>

<script>
export default {
  data() {
    return { visible: false }
  },
  methods: {
    handleConfirm() {
      console.log('确认')
    },
    handleCancel() {
      console.log('取消')
    }
  }
}
</script>

Table 表格

vue
<template>
  <div>
    <lyt-table :columns="columns" :data="data" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { prop: 'name', label: '姓名' },
        { prop: 'age', label: '年龄' },
        { prop: 'address', label: '地址' }
      ],
      data: [
        { name: '张三', age: 20, address: '北京市朝阳区' },
        { name: '李四', age: 25, address: '上海市浦东新区' },
        { name: '王五', age: 30, address: '广州市天河区' }
      ]
    }
  }
}
</script>

🎨 主题系统

使用 ThemeProvider 组件提供主题支持:

vue
<template>
  <lyt-theme-provider :theme="theme">
    <div class="app">
      <!-- 你的内容 -->
    </div>
  </lyt-theme-provider>
</template>

<script>
export default {
  data() {
    return {
      theme: 'light' // 'light' 或 'dark'
    }
  }
}
</script>

切换主题

typescript
import { useTheme } from '@lytjs/components'

// 获取当前主题
const currentTheme = useTheme()

// 切换主题
currentTheme.value = 'dark' // 或 'light'

自定义主题

typescript
import { ThemeProvider } from '@lytjs/components'

const customTheme = {
  colors: {
    primary: '#1890ff',
    success: '#52c41a',
    warning: '#faad14',
    danger: '#f5222d'
  }
}

📚 更多示例

查看完整的组件展示示例,请到 examples/showcase-app,包含所有组件的详细用法和示例代码。


🎉 **组件库状态完整可用,包含 38+ 个精心设计的组件!

基于 MIT 许可发布