安全简单的、与 Nuxt.js 集成的 Axios。
$axios
对象上暴露 setToken
函数方法,我们能轻而易举的设置认证 tokens
withCredentials
特性
支持 Proxy Module
自动重试机制 axios-retry
使用 npm:
npm install @nuxtjs/axios
使用 yarn:
yarn add @nuxtjs/axios
module.exports = {
modules: [
'@nuxtjs/axios'
],
axios: {
// proxyHeaders: false
}
}
asyncData
async asyncData ({ app }) {
const ip = await app.$axios.$get('http://icanhazip.com')
return { ip }
}
methods
/created
/mounted
/etc
methods: {
async fetchSomething() {
const ip = await this.$axios.$get('http://icanhazip.com')
this.ip = ip
}
}
async nuxtServerInit ({ commit }, { app }) {
const ip = await app.$axios.$get('http://icanhazip.com')
commit('SET_IP', ip)
}
// In store
{
actions: {
async getIP ({ commit }) {
const ip = await this.$axios.$get('http://icanhazip.com')
commit('SET_IP', ip)
}
}
}
如果你需要通过注册拦截器或者改变全局设置来定制化axios,你需要创建一个nuxt plugin。
nuxt.config.js
{
modules: [
'@nuxtjs/axios',
],
plugins: [
'~/plugins/axios'
]
}
plugins/axios.js
export default function ({ $axios, redirect }) {
$axios.onRequest(config => {
console.log('Making request to ' + config.url)
})
$axios.onError(error => {
const code = parseInt(error.response && error.response.status)
if (code === 400) {
redirect('/400')
}
})
}
Axios plugin provides helpers to register axios interceptors easier and faster.
These functions don’t have to return anything by default.
Example: (plugins/axios.js)
export default function ({ $axios, redirect }) {
$axios.onError(error => {
if(error.code === 500) {
redirect('/sorry')
}
})
}
Axios plugin also supports fetch style requests with $ prefixed methods:
// Normal usage with axios
let data = (await $axios.get('...')).data
// Fetch Style
let data = await $axios.$get('...')
Axios 对象非常方面设置header部分
参数:
// Adds header: `Authorization: 123` to all requests
this.$axios.setHeader('Authorization', '123')
// Overrides `Authorization` header with new value
this.$axios.setHeader('Authorization', '456')
// Adds header: `Content-Type: application/x-www-form-urlencoded` to only post requests
this.$axios.setHeader('Content-Type', 'application/x-www-form-urlencoded', [
'post'
])
// Removes default Content-Type header from `post` scope
this.$axios.setHeader('Content-Type', false, ['post'])
Axios实例可以方便的设置全局头信息
Parameters:
// Adds header: `Authorization: 123` to all requests
this.$axios.setToken('123')
// Overrides `Authorization` header with new value
this.$axios.setToken('456')
// Adds header: `Authorization: Bearer 123` to all requests
this.$axios.setToken('123', 'Bearer')
// Adds header: `Authorization: Bearer 123` to only post and delete requests
this.$axios.setToken('123', 'Bearer', ['post', 'delete'])
// Removes default Authorization header from `common` scope (all requests)
this.$axios.setToken(false)
在 nuxt.config.js 中,你可以使用模块选项 或 axios section 传递选项。
用来配置 baseURL and browserBaseURL部分。
可以使用 API_PREFIX, API_HOST (or HOST) and API_PORT (or PORT) 环境变量.
prefix的默认值是/.
Base URL 是用于在 server side 模式下的请求配置.
环境变量 API_URL 可以覆盖 override baseURL设置
Base URL 适用于客户端模式下的配置.
环境变量 API_URL_BROWSER 可以覆盖 override browserBaseURL.
If set to true, http:// in both baseURL and browserBaseURL will be changed into https://.
和 Nuxt.js progress bar 一起用于显示loading状态 (仅在浏览器上,且loading bar可用)
你可以禁止适用这项配置
this.$axios.$get('URL', { progress: false })
你可以很容易的将 Axios 与代理模块集成在一起,并推荐用于CORS和部署问题。
nuxt.config.js
{
modules: [
'@nuxtjs/axios'
],
axios: {
proxy: true // Can be also an object with default options
},
proxy: {
'/api/': 'http://api.example.com',
'/api2/': 'http://api.another-website.com'
}
}
Note:不需要手动注册@nuxtjs/proxy模块,但它确实需要在您的依赖项中。
Note:/api/将被添加到api端点的所有请求中。如果需要删除,请使用pathrewite:
proxy: {
'/api/': { target: 'http://api.example.com', pathRewrite: {'^/api/': ''} }
}
自动拦截失败的请求,并在使用 axios-retry 时,对它们进行重试。
默认情况下,如果retry value设置为true,则重试次数将为3次。您可以通过传递这样的对象来更改它:
axios: {
retry: { retries: 3 }
}
当请求到允许传递验证头后端的基础节点时,添加一个拦截器来自动设置AXIOS的凭据配置。
向日志请求和响应添加侦听器。
在SSR上下文中,将客户端请求头设置为AXIOS默认请求头。这对于在服务器端请求基于Cookie的Autho的请求是有用的。也有助于在SSR和客户端代码中做出一致的请求。
NOTE:如果将请求定向到受CloudFlare的CDN保护的url,则应将此设置为false,以防止CloudFlare错误地检测到反向代理循环并返回403错误。
只有当proxyHeaders设置为true时才有效。删除不需要的请求头到SSR中的API后端。
ECharts 象形柱图属性type在象形柱图中 type 属性值为 'pictorialBar'。name系列名称,用于tooltip的显示,legend的图例...
AngularJS 简介 AngularJS 是一个 JavaScript 框架。它可通过 script 标签添加到 HTML 页面。 AngularJS 通过 指令 扩展了 HTML...
AngularJS 输入验证 AngularJS 表单和控件提供了验证功能,对与用户的输入进行判断,以辨别输入是否合法,如果不合法则进行警告...
通用指南如果你用 Vue CLI 处理静态资源并和后端框架一起作为部署的一部分,那么你需要的仅仅是确保 Vue CLI 生成的构建文件在正...
警告这份文档是对应@vue/cli。老版本的vue-cli文档请移步这里。Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供:通过@...