gpt4 book ai didi

vue.js - 在渲染组件之前从 api 获取数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:26 26 4
gpt4 key购买 nike

我在呈现页面之前发送了 2 个 api 请求:

const Profile = {
template: '#profile',
attributes: null,
photos: [],
data: function () {
return {attributes: Profile.attributes, photos: Profile.photos};
},
beforeRouteEnter: function (to, from, next) {
function getProfile() {
return axios.get('user/get-profile?access-token=1', {responseType: 'json'});
}
function getPhotos() {
return axios.get('photos?access-token=1', {responseType: 'json'});
}

axios.all([getProfile(), getPhotos()])
.then(axios.spread(function (profile, photos ) {
console.log(profile, photos );
next(vm => {
vm.setProfile(profile);
vm.setPhotos(photos);
})
}));
},
methods: {
setProfile: function (response) {
Profile.attributes = response.data;
console.log(Profile.attributes);
},
setPhotos: function (response) {
Profile.photos = response.data;
console.log(response);
},
}
};

问题是渲染发生在 setProfilesetPhotos 方法之前。如何更正渲染我的组件?

最佳答案

如评论中所述,第一个 async/await 解决方案有点误导,因为所有生命周期方法都是同步的。这是有效的,因为代码被转换为内部有 IIFE 的同步函数。

下面我添加了一些最近的片段。

旧答案

用 async/await 试试。我删除了 beforeRouteEnteraxios.spread 并添加了 create

const Profile = {
template: '#profile',
attributes: null,
photos: [],
data() {
return {
attributes: null,
photos: null,
};
},
async created() {
const getProfile = await axios.get('user/get-profile?access-token=1');
const getPhotos = await axios.get('photos?access-token=1');

this.setProfile(profile);
this.setPhotos(photos);
},
methods: {
setProfile(response) {
this.attributes = response.data;
console.log(this.attributes);
},
setPhotos(response) {
this.photos = response.data;
console.log(response);
},
},
};

更短

const Profile = {
template: '#profile',
attributes: null,
photos: [],
data() {
return {
attributes: null,
photos: null,
};
},
async created() {
this.attributes = await axios.get('user/get-profile?access-token=1');
this.photo = await axios.get('photos?access-token=1');
},
};

更新的答案

您可以在生命周期方法中使用异步函数。

const Profile = {
template: '#profile',
attributes: null,
photos: [],
data() {
return {
attributes: null,
photos: null,
};
},
created() {
const fetchData = async () => {
const { data: attributes } = await axios.get(
'user/get-profile?access-token=1'
);
const { data: photos } = await axios.get('photos?access-token=1');

this.attributes = attributes;
this.photos = photos;
};

fetchData();
},
};

Vue 3 和 setup()

在 Vue 3 中,您可以使用 async setup()。如果你使用它,你必须用 Suspense 包装你的组件。警告!此 API 目前实验 https://vuejs.org/guide/built-ins/suspense.html#suspense= .

<Suspense>
<template #default>
<YourComponent />
</template>
<template #fallback>
<div>Loading ...</div>
</template>
</Suspense>
export default {
name: 'YourComponent',
async setup() {
const { data: attributes } = await axios.get('user/get-profile?access-token=1');
const { data: photos } = await axios.get('photos?access-token=1');

return {
attributes,
photos
}
}
}

关于vue.js - 在渲染组件之前从 api 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com