---
url: /reference/interface/ui-components/code.md
---
# Code

用于在文本中展示行内代码，以及多行代码块。

## 使用

::: code-group

```ts [Angular]
import { NxCode, NxCodeBlock } from "@pc-nexus/angular";
```

```ts [React]
import { NxCode, NxCodeBlock } from "@pc-nexus/react";
```

:::

## 属性

### Code

:::comp-props

| 名称   | 类型     | 必填 | 描述         |
| ------ | -------- | ---- | ------------ |
| `text` | `string` |      | 行内代码内容 |

:::

### CodeBlock

:::comp-props

| 名称              | 类型      | 必填 | 描述                                                        |
| ----------------- | --------- | ---- | ----------------------------------------------------------- |
| `text`            | `string`  |      | 代码块内容                                                  |
| `language`        | `string`  |      | 代码语言，默认 `text`                                       |
| `showLineNumbers` | `boolean` |      | 是否显示行号，默认 `false`                                  |
| `autoWrap`        | `boolean` |      | 是否自动换行，默认 `false`                                  |
| `highlight`       | `string`  |      | 高亮行，逗号分隔行号或范围，如 `"3"`、`"1-5"`、`"1-5,7,10"` |

:::

## 示例

### Code

行内代码的默认用法，通过 `text` 展示短代码片段。

:::demo src=code-basic
:::

### Code Block

代码块可以突出显示整段代码，同时保留其格式。

:::demo src=code-block-basic
:::

### Code Block line numbers

通过 `showLineNumbers` 显示代码块的行号。

:::demo src=code-block-show-line-numbers
:::

### Code Block auto wrap

通过 `autoWrap` 控制代码块中的长行是否自动换行。

:::demo src=code-block-auto-wrap
:::

### Code Block highlight

通过 `highlight` 高亮指定行或行范围。

:::demo src=code-block-highlight
:::
