首页 > 解决方案 > 如何在Vue中使用不同文件中的函数

问题描述

我是 vue 的新手,我正在尝试使用一个辅助函数,即我导入的其中一个 .vue 文件中的 arrayDateFormatter。我试图在内部使用它,mounted()但它永远不会被调用,之后什么也没有。我在控制台中没有收到任何错误

我的 vue 文件

<template>
.....
</template>

<script>
import archives from "@/4.objects/o-archives";
import fullBanner from "@/4.objects/o-full-banner";
import speakerInfo from "@/4.objects/o-speaker-info";
import newsletter from "@/4.objects/o-newsletter";
import dataLoaderMixin from "@/mixins/dataLoader-mixin";
import arrayDateFormatter from "../mixins/gloabalEventsDateFormate-mixin";

export default {
  components: {
   ....
  },
  mixins: [dataLoaderMixin],
  data() {
    return {
      speakers: [],
      talkInfo: null,
      archives: []
    };
  },
  async mounted() {
    try {
      const response = await this.fetchEventsByProject();
      console.log(response.data.data.listOnlineEvents.items);
      //nothing gets triggered after here
      const currentEvent = arrayDateFormatter(response);
      console.log('current event');
      console.log(currentEvent);
      this.archives = this.fetchData("archive");
    } catch (err) {
      return err
    }
  }
};
</script>

辅助函数

export const arrayDateFormatter = (response)=> {
 //some code i have tested and it works by itself
}

标签: javascriptvue.js

解决方案


看起来我必须以这种方式导入文件

import {arrayDateFormatter} from "../mixins/gloabalEventsDateFormate-mixin";

推荐阅读