css/no-hardcoded-values
建议使用 CSS 变量代替硬编码值。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
warning |
opinionated, nuxt |
不支持 | 无 |
适用范围:纯 CSS 的 style 块(scss、sass、less 和 stylus 块会被跳过)。
错误示例
按钮将间距数字和十六进制颜色直接写入声明。
<style scoped>
.button {
padding: 12px 16px;
color: #174ea6;
}
</style>
正确示例
声明引用具名的间距和颜色自定义属性,使这些值可作为设计令牌维护。
<style scoped>
.button {
padding: var(--space-3) var(--space-4);
color: var(--color-action-text);
}
</style>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"css/no-hardcoded-values": "warn"
}
},
});
然后运行 vp run lint。