2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00
Files
axios/docs/zh/pages/advanced/config-defaults.md
T
Jay 13fdbec872 chore: docs and post release improvements (#10841)
* chore: update changelog and update gitignore

* chore: update PR and issue templates

* chore: updated docs

* chore: update all docs to match

* chore: update both files to match

* chore: remove un-needed yml

* Update docs/fr/pages/advanced/error-handling.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/es/pages/advanced/x-www-form-urlencoded-format.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/pages/advanced/error-handling.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/es/pages/advanced/error-handling.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/pages/advanced/x-www-form-urlencoded-format.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/zh/pages/advanced/x-www-form-urlencoded-format.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/zh/pages/advanced/request-config.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/zh/pages/advanced/request-config.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/pages/advanced/request-config.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/fr/pages/advanced/x-www-form-urlencoded-format.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/zh/pages/advanced/error-handling.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Update docs/zh/pages/advanced/request-config.md

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
2026-05-04 19:57:05 +02:00

1.9 KiB

配置默认值

axios 允许你指定应用于每个请求的配置默认值,包括 baseURLheaderstimeout 等属性。下面是使用配置默认值的示例:

axios.defaults.baseURL = "https://jsonplaceholder.typicode.com/posts";
axios.defaults.headers.common["Authorization"] = AUTH_TOKEN;
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";

::: warning 全局请求头会被发送到所有主机 如果你的应用会与多个域名通信,设置 axios.defaults.headers.common["Authorization"] 会将令牌发送到所有域名,包括你可能无法控制的第三方 API。对任何携带凭据的客户端,请使用带有限定 baseURL自定义实例。 :::

自定义实例默认值

axios 实例在创建时会有自己的默认配置,这些默认配置可以通过修改实例的 defaults 属性来覆盖。下面是使用自定义实例默认值的示例:

var instance = axios.create({
  baseURL: "https://jsonplaceholder.typicode.com/posts",
  timeout: 1000,
  headers: { Authorization: "foobar" },
});

instance.defaults.headers.common["Authorization"] = AUTH_TOKEN;

配置优先级

配置将按照优先级顺序合并,依次为:库的默认值、实例的默认属性,最后是请求时传入的配置参数。下面通过示例说明优先级顺序。

首先,创建一个使用库提供的默认值的实例。此时 timeout 配置值为 0,这是库的默认值。

const instance = axios.create();

接下来,将实例的 timeout 默认值覆盖为 2500 毫秒。此后,使用该实例的所有请求都将在 2.5 秒后超时。

instance.defaults.timeout = 2500;

最后,发起一个 timeout 为 5000 毫秒的请求,该请求将在 5 秒后超时。

instance.get("/longRequest", {
  timeout: 5000,
});