[英]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:
代码如下所示:
<script>
import gpbApi from '@/utils/api/gpbApi';
methods: {
async load() {
const result = await gpbApi.catalogProducts.getList(this.entityId);
this.typesOfApplication = result;
},
import catalogProducts from './catalogProducts';
export default {
catalogProducts,
};
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.