当前位置:网站首页>Asyncdata cross domain error after nuxt route switching
Asyncdata cross domain error after nuxt route switching
2022-07-24 09:03:00 【Potato coder】
Nuxt After route switching asyncData Cross domain error reporting
background
according to Nuxt Official documents Explanation
asyncDataThe method will be in the component ( Limited to page components ) Called before each load . It can be invoked before server or routing updates . But this method does not render from the server every time , Only when the page is refreshed can it be rendered from the server , When the client clicks the link to jump, it does not render from the server , But with the standard Vue Same as single page application , Render from the client .
Be careful :
- Can only be used in page components
asyncData, That is to say pages Under folder .vue You can use , Non page components cannot callasyncDataMethod- No,
this, becauseasyncDataIt is called before component initialization , So there is no way to pass in the methodthisTo refer to an instance object of the component
So that's why every time we click route jump , If our target page has asyncData When getting asynchronous data , Cross domain errors will be reported on the browser
// pages/index.vue
async asyncData({
$axios}) {
const {
result} = await $axios.get('http://localhost:3333/home/userinfo')
return {
userInfo: result.user_info
}
},
npm run dev
Start the project and run in http://localhost:3000
Start a local node Service provider interface , Service running on http://localhost:3333
Click route jump and cross domain errors will appear as follows :

Refresh and the error will disappear , And you can get asynchronous data normally .
Solving cross domain solutions
npm i @nuxtjs/axios @nuxtjs/proxy
// plugins/request.js
// It is used to unify the data of the return interface
export default function({
app: {
$axios}}) {
$axios.onResponse(response => {
return response.data
})
}
// nuxt.config.js
plugins: [
'@/plugins/request'
],
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
axios: {
proxy: true,
credential: true
},
proxy: {
'/api/': {
target: 'http://localhost:3333/',
pathRewrite: {
'^/api/': '/',
changeOrigin: true
}
}
},
// pages/index.vue
async asyncData({
$axios}) {
const {
result} = await $axios.get('/home/userinfo')
return {
userInfo: result.user_info
}
},
Revised nuxt.config.js Remember to restart the content
How to use asynchronous data in components
If the component is not a page component bound to the route , In principle, asynchronous data cannot be used . because Nuxt.js Just extend the
dataMethod , It can support asynchronous data processing .
For non page components , There are two ways to achieve asynchronous data acquisition :
- In the component's
mountedMethod to realize the logic of asynchronous data acquisition , Then set the data of the component , The limitation is : Server side rendering is not supported .- In the of page components
asyncDataorfetchMethod API call , And use the data aspropsPass to subcomponent . Server rendering is working properly . shortcoming :asyncDataOr page extraction may not be very readable , Because it's loading data from other components . All in all , Which method to use depends on whether your application needs to support the server-side rendering of sub components .
边栏推荐
- How do tiktok merchants bind the accounts of talents?
- Rocky基础-Shell脚本基础知识
- TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
- Android系统安全 — 5.2-APK V1签名介绍
- How RPC callers implement asynchronous calls: completable future
- Unity solves the package manager "you see to be offline"
- TT ecosystem - cross border in-depth selection
- The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software
- 0 threshold takes you to know two-point search
- Scatter chart - date
猜你喜欢

Opencv Chinese document 4.0.0 learning notes (updating...)

redis学习一redis介绍及NIO原理介绍

Why does TCP shake hands three times instead of two times (positive version)

【翻译】使用gRPC和REST的微服务架构中的集成挑战

超全总结:Go语言如何操作文件

Ubuntu20.04 install MySQL 5.7

Cyclic multiple scatter

Learn the rxjs operator

链表——19. 删除链表的倒数第 N 个结点

OpenCV中文文档4.0.0学习笔记(更新中……)
随机推荐
Using OpenCV to do a simple face recognition
Online lover
Virtual machine terminator terminal terminator installation tutorial
脉脉网友出了道 Go 面试题,你能答对吗?
Promise基础总结
How do tiktok merchants bind the accounts of talents?
Typescript decorator
TT ecosystem - cross border in-depth selection
Xiaobai learns oauth2
03_ UE4 advanced_ illumination
科目1-2
After watching the documentary "pirate treasure on Adak Island", I thought of the lost treasure in Chinese history
Six pictures show you why TCP shakes three times?
利用opencv 做一个简单的人脸识别
pip3 带源安装大全
科目1-3
Advantages of using partitions
Treap
Functions of tiktok enterprise number
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2