本文档描述了 CRMEB 项目中 UniApp 移动端的代码规范,包括命名规范、代码风格、文件结构、组件开发等,旨在统一代码风格,提高代码质量和可维护性。
pages/index/、components/common/、utils/index.vue、login.vue、user_info.vueButton.vue、NavBar.vue、GoodsList.vueuser.js、request.js、util.jscommon.scss、theme.scssuserInfo、goodsList、isLoadingBASE_URL、MAX_COUNT、DEFAULT_PAGE_SIZEis 开头的驼峰命名法
isShow、isLoading、isLogingetUserInfo、submitForm、handleClickdata、methods、computed、watchcreated、mounted、beforeDestroyButton、NavBar、GoodsList<my-button>、<nav-bar>、<goods-list>/pages/index/index、/pages/user/login.user-info、.goods-list、.btn-primary#app、#header、#footer示例:
<template>
<view class="container">
<text>{{ message }}</text>
</view>
</template>
vue
<view
class="container"
:class="{ active: isActive }"
@click="handleClick"
>
<text>{{ message }}</text>
</view>
代码块换行: 逻辑代码块应该换行
示例:
if (condition) {
// 代码块
} else {
// 代码块
}
a + b、x = y、i < 10[1, 2, 3]、{ name: '张三', age: 18 }if (condition)、function (param)// 注释// 这是单行注释/* */ 注释文档注释: 使用 JSDoc 风格的注释
示例: ```javascript /**
@returns {Promise} 用户信息 */ async function getUserInfo(id) { // 代码 }
### 3.5 引号
- **字符串**: 使用单引号 `''`
- **示例**: `const name = '张三'`、`<text>Hello</text>`
- **模板字符串**: 使用反引号 `` ` ``
- **示例**: `` const url = `${baseUrl}/api/user` ``
- **HTML 属性**: 使用双引号 `""`
- **示例**: `<view class="container">`、`<image src="https://example.com/img.jpg">`
## 4. 文件结构
### 4.1 页面文件结构
```vue
<template>
<!-- 模板内容 -->
</template>
<script>
// 导入依赖
import userApi from '../../api/user';
// 导出组件
export default {
// 组件名称
name: 'UserInfo',
// 组件属性
props: {
userId: {
type: Number,
required: true
}
},
// 数据
data() {
return {
userInfo: {},
loading: false
};
},
// 计算属性
computed: {
fullName() {
return this.userInfo.firstName + ' ' + this.userInfo.lastName;
}
},
// 监听
watch: {
userId: {
handler(newVal) {
this.getUserInfo(newVal);
},
immediate: true
}
},
// 生命周期函数
created() {
// 初始化
},
mounted() {
// 挂载后
},
// 方法
methods: {
async getUserInfo(id) {
try {
this.loading = true;
const res = await userApi.getUserInfo(id);
this.userInfo = res.data;
} catch (error) {
console.error('获取用户信息失败:', error);
} finally {
this.loading = false;
}
},
handleEdit() {
// 编辑用户信息
}
}
};
</script>
<style scoped>
/* 样式 */
.user-info {
padding: 20rpx;
background-color: #f5f5f5;
}
.name {
font-size: 32rpx;
font-weight: bold;
}
</style>
<template>
<!-- 模板内容 -->
<view class="my-button">
<button
:class="[type, { disabled }]"
:disabled="disabled"
@click="handleClick"
>
<slot></slot>
</button>
</view>
</template>
<script>
export default {
name: 'MyButton',
props: {
type: {
type: String,
default: 'primary',
validator: (value) => {
return ['primary', 'success', 'warning', 'danger'].includes(value);
}
},
disabled: {
type: Boolean,
default: false
}
},
methods: {
handleClick() {
if (!this.disabled) {
this.$emit('click');
}
}
}
};
</script>
<style scoped>
.my-button {
display: inline-block;
}
button {
padding: 20rpx 40rpx;
border-radius: 8rpx;
font-size: 28rpx;
border: none;
outline: none;
cursor: pointer;
}
.primary {
background-color: #007aff;
color: #fff;
}
.success {
background-color: #4cd964;
color: #fff;
}
.warning {
background-color: #ff9500;
color: #fff;
}
.danger {
background-color: #ff3b30;
color: #fff;
}
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
</style>
// 导入依赖
import request from './request';
// 常量定义
const BASE_URL = 'https://api.crmeb.net';
const TIMEOUT = 10000;
// 工具函数
function formatTime(time) {
const date = new Date(time);
return date.toLocaleString();
}
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 导出
export {
formatTime,
getRandomNum
};
// 默认导出
export default {
BASE_URL,
TIMEOUT
};
import Button from '../../components/Button.vue'组件注册: 在 components 中注册组件
示例:
components: {
Button
}
<Button type="primary">点击</Button>async function getUserInfo() {
try {
this.loading = true;
const res = await userApi.getUserInfo();
this.userInfo = res.data;
} catch (error) {
console.error('获取用户信息失败:', error);
uni.showToast({
title: '获取用户信息失败',
icon: 'none'
});
} finally {
this.loading = false;
}
}
本文档描述了 CRMEB 项目中 UniApp 移动端的代码规范,包括命名规范、代码风格、文件结构、组件开发等。遵循本文档的规范,可以提高代码的可读性、可维护性和可扩展性,确保项目的质量和稳定性。
代码规范是团队协作的基础,建议开发团队成员严格遵循本文档的规范,共同维护一个高质量的代码库。