o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
<script setup lang="ts"> import { onMounted, ref, reactive, computed, watch } from 'vue'…
- User
帮我优化美化ui效果
- Assistant
帮我优化美化ui效果
当然,我可以帮助你优化和美化你的 Vue 组件的 UI。以下是一些改进建议和优化后的代码示例:
布局优化:
van-row 和 van-col 布局,实现响应式设计。van-card)中,使界面更加整洁。增强可用性:
van-toast 提示用户操作结果或错误信息。视觉美化:
代码优化:
Array<any>,提高代码的可维护性。下面是优化后的完整代码示例:
<script setup lang="ts">
import { onMounted, ref, reactive, computed, watch } from 'vue'
import { Pixihttp } from '../systems/http.ts'
import {
Toast
} from 'vant'
// 定义模型类型的枚举
enum ModelCategory {
StableDiffusionXL = 'stable_diffusion_xl',
Flux = 'flux'
}
const checkedType = ref<ModelCategory>(ModelCategory.StableDiffusionXL)
const modelId = ref('')
// 使用 reactive 创建响应式数组,并定义具体类型
interface Model {
model_id: string
model_name: string
model_category: ModelCategory
}
const stableDiffusionXLModels = reactive<Model[]>([])
const fluxModels = reactive<Model[]>([])
// 分页状态
const currentPage = ref(1)
const itemsPerPage = 50
// 加载状态
const isLoading = ref(false)
const error = ref<string | null>(null)
// 监听 checkedType 变化,重置 currentPage
watch(checkedType, () => {
currentPage.value = 1
})
// 计算总页数
const totalPages = computed(() => {
const totalItems =
checkedType.value === ModelCategory.StableDiffusionXL
? stableDiffusionXLModels.length
: fluxModels.length
return Math.ceil(totalItems / itemsPerPage)
})
// 计算当前页显示的模型
const paginatedModels = computed(() => {
const models =
checkedType.value === ModelCategory.StableDiffusionXL
? stableDiffusionXLModels
: fluxModels
const start = (currentPage.value - 1) * itemsPerPage
const end = start + itemsPerPage
return models.slice(start, end)
})
// 获取模型列表
const fetchModelList = async () => {
isLoading.value = true
error.value = null
try {
const httpData = await Pixihttp.get_model_list()
for (let entry of httpData.data) {
if (entry.model_category === ModelCategory.StableDiffusionXL) {
stableDiffusionXLModels.push(entry)
} else if (entry.model_category === ModelCategory.Flux) {
fluxModels.push(entry)
}
}
} catch (err) {
console.error(err)
error.value = '获取模型列表失败,请稍后重试。'
Toast.fail(error.value)
} finally {
isLoading.value = false
}
}
onMounted(() => {
fetchModelList()
})
</script>
<template>
<div class="root">
<van-row justify="center" class="header">
<van-col span="24">
<van-card class="title-card" shadow="hover">
<div class="title">模型测试</div>
</van-card>
</van-col>
</van-row>
<van-row justify="center" class="controls">
<van-col
:span="24"
:xs="24"
:sm="20"
:md="16"
:lg="12"
>
<van-card flat>
<van-row gutter="16" align="middle">
<van-col span="24" sm="12">
<van-radio-group v-model="checkedType" direction="horizontal">
<van-radio :name="ModelCategory.StableDiffusionXL">SD-XL</van-radio>
<van-radio :name="ModelCategory.Flux">FLUX</van-radio>
</van-radio-group>
</van-col>
<van-col span="24" sm="12">
<van-field
v-model="modelId"
label="模型ID:"
left-icon="smile-o"
right-icon="search"
placeholder="输入模型ID"
confirm-type="search"
@click-right="searchModel"
/>
</van-col>
</van-row>
</van-card>
</van-col>
</van-row>
<van-row justify="center" class="model-list">
<van-col
:span="24"
:xs="24"
:sm="20"
:md="16"
:lg="12"
>
<van-card flat>
<div v-if="isLoading" class="loading-container">
<van-loading type="spinner" size="24px" />
</div>
<div v-else>
<v-list v-if="paginatedModels.length">
<v-list-item
v-for="item in paginatedModels"
:key="item.model_id"
class="model-item"
>
<div class="model-item-content">
<div class="model-name">{{ item.model_name }}</div>
<div class="model-id">ID: {{ item.model_id }}</div>
</div>
</v-list-item>
</v-list>
<div v-else class="no-data">
暂无模型数据
</div>
<v-pagination
v-if="totalPages > 1"
v-model="currentPage"
:length="totalPages"
mode="simple"
class="pagination"
></v-pagination>
</div>
</van-card>
</van-col>
</van-row>
</div>
</template>
<style scoped>
.root {
padding: 20px;
background-color: #f5f5f5;
min-height: 100vh;
}
.header {
margin-bottom: 20px;
}
.title-card {
text-align: center;
padding: 20px 0;
background-color: #ffffff;
}
.title {
font-size: 24px;
font-weight: bold;
color: #333333;
}
.controls {
margin-bottom: 20px;
}
.model-list {
margin-top: 10px;
}
.loading-container {
display: flex;
justify-content: center;
align-items: center;
height: 150px;
}
.no-data {
text-align: center;
color: #999999;
padding: 20px 0;
}
.model-item {
border-bottom: 1px solid #eee;
padding: 10px 0;
}
.model-item-content {
display: flex;
flex-direction: column;
}
.model-name {
font-size: 16px;
color: #333333;
}
.model-id {
font-size: 14px;
color: #888888;
}
.pagination {
display: flex;
justify-content: center;
margin-top: 16px;
}
/* 响应式调整 */
@media (max-width: 768px) {
.title {
font-size: 20px;
}
.model-name {
font-size: 14px;
}
.model-id {
font-size: 12px;
}
}
</style>
布局和结构:
van-card 包裹标题,增加阴影效果 (shadow="hover") 使其突出。van-row 和 van-col 设置间距和对齐方式。van-card 包裹列表内容,增加加载状态和无数据提示。增强可用性:
isLoading 状态,当数据正在加载时显示 van-loading 组件。Toast.fail 提示用户加载数据失败。searchModel 方法)。视觉美化:
#f5f5f5) 和卡片背景色 (#ffffff),增强对比度。代码优化:
Model 接口,替代 Array<any>,提高代码的类型安全性。enum 定义模型类别,增强代码的可读性和维护性。fetchModelList 方法中,并在 onMounted 生命周期钩子中调用。响应式设计:
van-col 的响应式属性 (:xs, :sm, :md, :lg) 确保在不同屏幕尺寸下布局自适应。modelId 过滤 paginatedModels。vue-i18n)管理文本内容。通过以上优化,你的 Vue 组件将会拥有更好的用户体验和更美观的界面。如果你有特定的设计需求或进一步的功能需求,欢迎继续交流!
Not yet included in a published release.