简体   繁体   English

尝试调用 API function 时收到错误消息

[英]I've got the error message when try to call API function

I have Vue.js app and call API function from Vue component.我有 Vue.js 应用程序并从 Vue 组件调用 API function。 The code looks as following:代码如下所示:

  1. component.vue:组件.vue:
<script>
import gpbApi from '@/utils/api/gpbApi';

methods: {
    async load() {
        const result = await gpbApi.catalogProducts.getList(this.entityId);
        this.typesOfApplication = result;
    },
  1. gpbApi.js: gpbApi.js:
import catalogProducts from './catalogProducts';

export default {
    catalogProducts,
};
  1. catalogProducts.js:目录Products.js:
import axios from 'axios';
axios.defaults.baseURL = 'http://localhost/client/';

export async function getList(entityId) {
    if (entityId == null) return;
    const { data } = await axios.get(`api/getList?id=${entityId}`);
    return data;
}

gpbApi.js and catalogProducts.js files are placed in /utils/api/ folder. gpbApi.js 和 catalogProducts.js 文件放在/utils/api/文件夹中。

I've got the following error:我有以下错误:

mounted hook (Promise/async) TypeError: Cannot read properties of undefined (reading 'getList')
    at VueComponent.load (component.vue:103:1)

How to overcome this issue?如何克服这个问题?

You are using a default import although you didn't declare the default export.尽管您没有声明默认导出,但您正在使用默认导入。

Either use:要么使用:

import * as gpbApi from '@/utils/api/gpbApi';

or或者

import { getList } from '@/utils/api/gpbApi';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用 for 循环时,我尝试重新分配变量的值并再次使用该变量,最终,我得到了变量的先前值? - When I use for loop i try to reassign value of a variable and use the variable again and ultimately, I've got the previous value of the variable? 当我尝试搜索时,我的 jekyll/github 网站出现错误 - My jekyll/github website got error when i try to search 即使使用本地数组,我也会遇到 jQuery 自动完成 JSON 解析错误 - I've got a jQuery autocomplete JSON Parse error even when using a local array 我有一个 javascript 数组,想按名称调用图像 - I've got a javascript array and want to call images by name 为什么在尝试将作为文本接收的正文分配给 javascript 中的变量时出现未捕获类型错误:response.text 不是函数”错误 - why do I got Uncaught TypeError: response.text is not a function" error when try to assignt body received as text to a variable in javascript 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function? 使用param调用函数时出现错误 - I got the error when calling a function with param 我创建了这个 React 应用程序来获取我创建的 API,但收到错误消息“TypeError: video.map is not a function”。 下面是代码: - I created this React application to a fetch API that I created, but got the error message "TypeError: video.map is not a function". Below is the code: 当我尝试添加 API 密钥时,出现错误:TypeError: items.map is not a function - I get the error: TypeError: items.map is not a function, when I try to add my API key 尝试创建对象并调用函数时未定义函数 - function not defined when I try to create an object and call function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM