typescript - TypeScript 导入的常量未定义
问题描述
在我的 Vue.ts(带有 TypeScript 的 Vue.js)中,我serverUrl
在一个文件(http 声明)中定义了一个常量,并将其导入到另一个带有 class 的文件中AuthService
。但是这个常量UNDEFINED
在属性声明或构造函数中AuthService
。在login()
功能上是可以的。有什么问题?这是我的文件。网址:
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
export const serverUrl = 'http://localhost:63523'; // serverUrl Constant
export const http = axios.create({ timeout: 20000, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json' } });
和 AuthService:
import { http, serverUrl } from '@/services/http';
export class AuthService {
private authUrl: string = serverUrl + '/' + 'auth/login'; // serverUrl = UNDEFINED
constructor() {
this.authUrl = `${serverUrl}/auth/login`; // serverUrl = UNDEFINED
}
public login(login: string, password: string ) {
const authUrl2: string = serverUrl + '/' + 'auth/login'; // serverUrl = OK!
return http.post(authUrl2, {login, password});
}
}
export const authService = new AuthService();
解决方案
正如评论中所讨论的,变量serverUrl
未正确注入文件中,AuthService
因为这两个文件之间存在循环依赖关系(从问题中提供的代码中看不到)。
解决方案是消除这两个文件之间的循环依赖,变量serverUrl
将被正确导入。
推荐阅读
- c# - .NET API 将文档添加到 .nsf HCL Lotus Notes 数据库?
- woocommerce-rest-api - 如何通过 woocomerce api 的元数据检索数据
- forms - 带有条件 m:n 相关对象的 Symfony 表单
- livecharts - LiveCharts - 垂直或瀑布线图
- flutter - 当我按下按钮时,显示此错误
- python - matplotlib 库可以告诉我传递的数据的功能吗?
- node.js - 使用passport.js 和带有supertest 的HttpOnly cookie 的身份验证不起作用
- javascript - 从子组件返回值到父组件 React
- javascript - 大写单词的第一个字母超过3个字符的正则表达式
- sql - 使用正则表达式匹配选择字符串的特定部分