`,这将使 UnoCSS 生成的 CSS 无法匹配属性。为了解决这个问题,您可能想要尝试 [`transformer-attributify-jsx`](/transformers/attributify-jsx) 以及该预设。
:::
## 属性冲突
如果属性模式的名称与元素或组件的属性发生冲突,您可以添加 `un-` 前缀以特指 UnoCSS 的属性模式。
例如:
```html
这与链接的 `text` 属性冲突
文本颜色为红色
```
前缀默认是可选的,如果您想强制使用前缀,可以设置
```ts
presetAttributify({
prefix: 'un-',
prefixedOnly: true, // <--
})
```
您还可以通过以下方式禁用对某些属性的扫描:
```ts
presetAttributify({
ignoreAttributes: [
'text'
// ...
]
})
```
## TypeScript 支持 (JSX/TSX)
创建 `shims.d.ts`,内容如下:
> 默认情况下,类型包含来自 `@unocss/preset-wind3` 的常见属性。如果您需要自定义属性,请参考 [类型源](https://github.com/unocss/unocss/blob/main/packages-presets/preset-attributify/src/jsx.ts) 来实现您自己的类型。
### Vue
自 Volar 0.36 起,[它现在对未知属性是严格的](https://github.com/johnsoncodehk/volar/issues/1077#issuecomment-1145361472)。要取消严格,可以向项目中添加以下文件:
```ts [html.d.ts]
declare module '@vue/runtime-dom' {
interface HTMLAttributes {
[key: string]: any
}
}
declare module '@vue/runtime-core' {
interface AllowedComponentProps {
[key: string]: any
}
}
export {}
```
### React
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'react' {
interface HTMLAttributes
extends AttributifyAttributes {}
}
```
### Vue 3
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module '@vue/runtime-dom' {
interface HTMLAttributes extends AttributifyAttributes {}
}
```
### SolidJS
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'solid-js' {
namespace JSX {
interface HTMLAttributes extends AttributifyAttributes {}
}
}
```
### Svelte 与 SvelteKit
```ts
declare namespace svelteHTML {
import type { AttributifyAttributes } from '@unocss/preset-attributify'
type HTMLAttributes = AttributifyAttributes
}
```
### Astro
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare global {
namespace astroHTML.JSX {
interface HTMLAttributes extends AttributifyAttributes { }
}
}
```
### Preact
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare module 'preact' {
namespace JSX {
interface HTMLAttributes extends AttributifyAttributes {}
}
}
```
### Marko
```ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'
declare global {
namespace Marko {
interface HTMLAttributes extends AttributifyAttributes {}
}
}
```
### 带前缀的属性
```ts
import type { AttributifyNames } from '@unocss/preset-attributify'
type Prefix = 'uno:' // 将其更改为您的前缀
interface HTMLAttributes extends Partial, string>> {}
```
## 选项
### strict
* **类型:** `boolean`
* **默认值:** `false`
仅为属性或类生成 CSS。
### prefix
* **类型:** `string`
* **默认值:** `'un-'`
属性模式的前缀。
### prefixedOnly
* **类型:** `boolean`
* **默认值:** `false`
仅匹配带前缀的属性。
### nonValuedAttribute
* **类型:** `boolean`
* **默认值:** `true`
支持匹配无值属性。
### ignoreAttributes
* **类型:** `string[]`
要在提取中忽略的属性列表。
### trueToNonValued
* **类型:** `boolean`
* **默认值:** `false`
如果在 DOM 中表示的实际值为 `true`,无值属性也将匹配。此选项存在是为了支持将无值属性编码为 `true` 的框架。启用此选项将打破以 `true` 结尾的规则。
## 致谢
最初的想法来自 [@Tahul](https://github.com/Tahul) 和 [@antfu](https://github.com/antfu)。之前的 [Windi CSS 实现](https://windicss.org/posts/v30.html#attributify-mode) 由 [@voorjaar](https://github.com/voorjaar) 完成。