# @api-components/api-server-selector 中文文档

规范地址：https://npmcn.com/package/%40api-components/api-server-selector/

版本：0\.7\.2
npm 元数据更新时间：2026-04-07T19:23:43.971Z
中文译文更新时间：2026-11-06T13:12:31.198Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40api-components/api-server-selector
原文对照：https://npmcn.com/package/%40api-components/api-server-selector/?lang=original
项目主页：https://github.com/advanced-rest-client/api-server-selector#readme
源码仓库：https://github.com/advanced-rest-client/api-server-selector

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：82.4 kB，20 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：10 个
- 首次发布：2020-03-30T19:58:45.497Z
- 最新版本发布：2021-10-09T21:57:32.848Z
- 近 30 天下载量：3156（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：29（近 12 个月 0 个）

## 中文简介

一个使OAS的服务器成为选择API服务器的下拉选项的组件.

## 安装

```sh
npm install @api-components/api-server-selector
```

也可以使用：`pnpm add @api-components/api-server-selector`、`yarn add @api-components/api-server-selector`、`bun add @api-components/api-server-selector`

## README

# 被否决

这一部分正在贬值。 代码基础已移动到 [amf 组件](https://github.com/advanced-rest-client/amf-components) 模块。 此模块将在 [PR 1 常规文件](https://github.com/advanced-rest-client/amf-components/pull/1) 合并.

* * *

自定义元素使一个服务器列表在API规格中编码,由AMF模型提供动力.

[![Published on NPM](https://img.shields.io/npm/v/@api-components/api-server-selector.svg)](https://www.npmjs.com/package/@api-components/api-server-selector)

[![Tests and publishing](https://github.com/advanced-rest-client/api-server-selector/actions/workflows/deployment.yml/badge.svg)](https://github.com/advanced-rest-client/api-server-selector/actions/workflows/deployment.yml)

## 版本兼容性

此版本仅与AMF模式版本2(AMF parser QQ 4.0.0)相通.

## 使用量

### 安装

```ssh
npm install --save @api-components/api-server-selector
```

### 在 html 文件内

```html
<html>
  <head>
    <script type="module">
      import '@api-components/api-server-selector/api-server-selector.js';
    </script>
  </head>
  <body>
    <api-server-selector amf="..."></api-server-selector>
  </body>
</html>
```

### 在一个LitElement中

```js
import { LitElement, html } from 'lit-element';
import '@api-components/api-server-selector/api-server-selector.js';

class SampleElement extends LitElement {
  render() {
    return html`
    <api-server-selector .amf="${this.model}"></api-server-selector>
    `;
  }
}
customElements.define('sample-element', SampleElement);
```

### 订阅更改事件

#### 服务器更改事件

该组件发送 `apiserverchanged` 服务器选择变更时的自定义事件.

```js
element.addEventListener('apiserverchanged', function(e) {
    const { value, type } = e.detail;
    // value is the selected base URI
    // type tells whether it's a server defined value of a custom property
    // This is also the same as const { value, type } = e.target;
});
```

何时 `Custom URL` 输入字段发送的更改 `api-server-changed` 带有输入当前值的事件.

此外,该要素支持 `onapiserverchange` 事件召回函数设置器 :

```javascript
element.onapiserverchange = (e) => {
  // ...
};
```

#### 服务器计数已更改的事件

当一些服务器变换时会发出此事件 .

```javascript
element.addEventListener('serverscountchanged', function(e) {
    const { value } = e.detail;
    // value is the number of rendered servers
});
```

```javascript
element.onserverscountchange = (e) => {
  // ...
};
```

### 自定义服务器

有时,除API sepcification中定义的服务器外,主机应用程序可能想要定义选择器中要渲染的附加服务器列表. 这可以涉及与代用服务或模拟服务等组件合作的其他服务.

元素接受列表项为在 API 定义服务器后被插入的子项目 . 我们建议使用 `anypoint-item` 因为它已经用来建造取精器。 然而,它可以是任何HTML元素. 要提供清单项目,必须 `slot="custom-base-uri"` 属性。 要在列表中选择项目,它必须具备 `value="..."` 属性. 如果缺少该值, 则该项会显示, 但选择器不会在列表项上单击 .

```html
<api-server-selector>
  <div slot="custom-base-uri">Other options</div>
  <anypoint-item slot="custom-base-uri" data-value="http://mocking.com">Mocking service</anypoint-item>
  <anypoint-item slot="custom-base-uri"><input type="checkbox"/> Configuration option</anypoint-item>
</api-server-selector>
```

当您决定使用一个没有数值的列表项目时,单击一个项目不会关闭选择器. 当用户执行与项目相关的动作时, 然后使用 `opened` 属性在选择器上关闭掉落.

```html
<api-server-selector>
  <div slot="custom-base-uri">Other options</div>
  <anypoint-item slot="custom-base-uri"><input type="checkbox" id="option"/> Configuration option</anypoint-item>
</api-server-selector>

<script>
option.onclick = (e) => {
  e.target.parentNode.opened = false;
};
</script>
```

## 发展

```sh
git clone https://github.com/advanced-rest-client/api-server-selector
cd api-server-selector
npm install
```

### 本地运行演示

```sh
npm start
```

### 运行测试

```sh
npm test
```

## 常见问题

### @api-components/api-server-selector 是什么？

一个使OAS的服务器成为选择API服务器的下拉选项的组件\.

### 如何安装 @api-components/api-server-selector？

在项目目录运行 npm install @api-components/api-server-selector，或使用 pnpm add / yarn add / bun add。

### @api-components/api-server-selector 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/api-components\_\_api-server-selector 是否存在。

### @api-components/api-server-selector 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @api-components/api-server-selector 可以商用吗？

@api-components/api-server-selector 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @api-components/api-server-selector 的下载量有多少？

根据 npm 官方下载统计，@api-components/api-server-selector 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,156 次，日均约 105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @api-components/api-server-selector 还在维护吗？

最新版本 0\.7\.2 发布于 2021/10/10（5 年前）；至今共发布 29 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/03/31。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

