CustomComponent 自定义组件
自定义功能的扩展组件
基本用法
<nut-custom-component>默认内容</nut-custom-component>
禁用状态
<nut-custom-component disabled>禁用状态</nut-custom-component>
Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| disabled | 是否禁用 | Boolean | false |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| custom-event | 点击时触发 | - |
Slots
| 名称 | 说明 |
|---|---|
| default | 组件内容 |
## 六、最佳实践与常见问题
### 6.1 性能优化策略
1. **组件懒加载**:
```javascript
// 路由懒加载
const routes = [
{
path: '/custom',
component: () => import('@/views/custom-page.vue')
}
];
// 组件懒加载
const LazyComponent = defineAsyncComponent(() =>
import('@/components/custom-component.vue')
);
- 避免不必要的渲染:
<template>
<nut-list>
<nut-list-item
v-for="item in list"
:key="item.id"
:title="item.title"
:content="item.content"
/>
</nut-list>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
// 使用computed缓存计算结果
const filteredList = computed(() => {
return list.value.filter(item => item.status === 'active');
});
</script>
6.2 常见问题解决方案
Q1: 扩展组件样式不生效?
A1: 检查是否正确使用scoped和穿透选择器:
// 错误
::v-deep .nut-button {
color: red;
}
// 正确(Vue3)
:deep(.nut-button) {
color: red;
}
Q2: 组件升级后自定义功能失效?
A2: 遵循以下升级检查清单:
- 查看CHANGELOG.md确认是否有API变更
- 使用
npm why @nutui/nutui检查依赖版本 - 清除node_modules和dist后重新安装
- 检查浏览器控制台是否有警告或错误
Q3: 如何调试NutUI源码?
A3: 使用源码调试模式:
# 克隆仓库
git clone https://gitcode.com/gh_mirrors/nu/nutui.git
# 安装依赖
cd nutui && pnpm install
# 启动开发服务器
pnpm dev
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



