TypeScript가 가져온 상수가 정의되지 않았습니다.
내 안에서Vue.ts(Vue.js with TypeScript) 상수를 정의했습니다.serverUrl한 파일(http 선언)에서 클래스와 함께 다른 파일로 가져옵니다.AuthService하지만 이 상수는UNDEFINED재산 신고 또는 건설업자 중 하나에서.AuthService.에서login()기능 괜찮아요.문제가 뭐죠?여기 제 파일들이 있습니다.http:
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' } });
인증 서비스:
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올바르게 가져옵니다.
저는 전혀 다른 문제를 가지고 있었습니다.저는 순환 수입/의존 관계가 전혀 없었습니다.
저의 경우, 저는 수입하고 있었습니다.MODULE A통index.ts파일(에서)MODULE B) 이미 사전 컴파일된 코드(in)dist폴더) 및package.json그것을 가리키며, 그것은 진짜와 신선함을 숨기고 있었습니다.index.ts.
솔루션은 물론 구축된 제거에 대한 것이었습니다.distTypeScript 컴파일러가 제대로 가져오는 폴더index.ts사후에
코드는 다음과 같습니다.
모듈 A
코드:
import { TAG_PROPERTY_NAME, IFormActivity } from "@colabo-flow/i-core";
// `undefined` as we were importing old `dist/index.js` file without `TAG_PROPERTY_NAME` exported, instead of the new `index/ts` file with `TAG_PROPERTY_NAME` exported
console.warn(`TAG_PROPERTY_NAME: ${TAG_PROPERTY_NAME}`);
tsconfig.json:
{
"compilerOptions": {
"paths": {
"@colabo-flow/i-core": [
"<REST_OF_THE_PATH>/puzzles/flow/core",
],
// ...
},
// ...
},
// ...
}
모듈 B
package.json:
{
"main": "dist/index.js",
"module": "dist/index.js",
// ...
}
index.ts:
export const TAG_PROPERTY_NAME = "tags";
// ...
언급URL : https://stackoverflow.com/questions/55219317/typescript-imported-constant-is-undefined
'programing' 카테고리의 다른 글
| Git 저장소를 종속성으로 포함하기 위해 setup.py 을 작성하는 방법. (0) | 2023.06.26 |
|---|---|
| 메이븐에서 여러 스프링 부트 애플리케이션에 대한 엔드 투 엔드 통합 테스트 (0) | 2023.06.26 |
| 두 Excel 파일의 모든 셀을 비교하는 VBA 매크로 (0) | 2023.06.26 |
| 운영체제 수준에서 수면은 어떻게 구현됩니까? (0) | 2023.06.26 |
| 그룹의 행 번호 증분 (0) | 2023.06.26 |