在呈现页面之前,我将发送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个方法之前.如何正确渲染我的组件?

推荐答案

As mentioned in the comments, the first async/await solution is a little bit misleading, because all lifecycle methods are synchronous.这是可行的,因为代码被传输到一个内部有IIFE的同步函数.

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

老生常谈

try 使用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);
    },
  },
};

Shorter

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 and setup()

在Vue 3中,可以使用async setup().如果你使用这个,你必须用Suspense包装你的组件.小心该API目前为experimental https://v3.vuejs.org/guide/migration/suspense.html.

<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相关问答推荐

使用 jquery-chosen 插件更新 vuejs 模型值

实现登录命令并访问 vuex 存储

无法在 Vuex 中的其他操作中反跳操作

在 vue 3 中使用 vue-chartjs:createElement 不是函数

使用 Vue Router 设置页面标题

在没有组件的vue js中拖放

Vuetify 在数据表中插入操作按钮并获取行数据

Vue3在'vue-router'中找不到导出'createWebHistory,createRouter'

如何在 Vue.js 中触发点击 ref

怎样1秒后自动隐藏VueJS中的元素

如何在 Node.js 服务器上部署 Vue.js 应用程序

如何让 Vue (vue cli 3) 正确处理 GraphQL 文件?

包含 Vue 组件的 HTML 字符串的 Nuxt 渲染函数

在加载数据之前触发mounted方法 - VueJS

基于媒体查询的vue绑定值

基本 vue.js 2 和 vue-resource http 获取变量赋值

输入文件 Select 事件在 Select 同一文件时未触发

Vue.js 拦截器

Vue CLI 3 sass-resources-loader - Options.loaders 未定义

Laravel 和 Vue - handler.call 不是函数