gpt4 book ai didi

javascript - 在另一个类中调用函数时, typescript 函数返回未定义

转载 作者:行者123 更新时间:2023-11-30 11:42:03 25 4
gpt4 key购买 nike

<分区>

在我的Angular 2 项目中我有这个类:

export class ApiEnvConf {

/**
* @type {Object}
*/
public _dev: {environnement: string, basePath: URL};

/**
* @type {Object}
*/
public _staging: {environnement: string, basePath: URL};

/**
* @type {Object}
*/
public _prod: {environnement: string, basePath: URL};

constructor(){
this._dev;
this._staging;
this._prod;
}

/**
* @return {Object}
*/
get devEnv(): {"environnement": string, "basePath": URL} {
return this._dev;
}

/**
* @return {Object}
*/
get stagingEnv(): {"environnement": string, "basePath": URL} {
return this._staging;
}

/**
* @return {Object}
*/
get prodEnv(): {"environnement": string, "basePath": URL} {
return this._prod;
}

/**
* @param {string} environnement
* @param {URL} basePath
* @return {Object}
*/
set devEnv(newDevEnvArray: {environnement: string, basePath: URL}){
this._dev = newDevEnvArray;
}

/**
* @param {string} environnement
* @param {URL} basePath
* @return {Object}
*/
set stagingEnv(newStagingEnvArray: {environnement: string, basePath: URL}) {
this._staging = newStagingEnvArray;
}

/**
* @param {string} environnement
* @param {URL} basePath
* @return {Object}
*/
set prodEnv(newProdEnvArray: {environnement: string, basePath: URL}) {
this._prod = newProdEnvArray;
}
}

如您所见,我导出此类是为了在其他文件(其他类)中使用它。下面是一个使用此类的文件示例:

import { Injectable, Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { ApiEnvConf } from './api-env-conf-class';

@Injectable()
@Component({
providers: [ ApiEnvConf ]
})
export class ImportConfig {

/**
* @type {ApiEnvConf}
*/
public envParam = new ApiEnvConf();

constructor(
public http: Http
) {}

loadEnvData() {
this.http.get('assets/env-config/parameters.json')
.map((res: Response) => (res.json()))
.subscribe(
data => {
this.envParam = this.instanciateApiEnvConfClass(data);
return this.envParam;
},
err => {
console.log("oops !");
return err;
}
)
}

instanciateApiEnvConfClass(data: Object){
let apiEnvConf = new ApiEnvConf();
// set dev by calling getter of ApiEnvConf class
apiEnvConf.devEnv = {
environnement: data["dev"]["environnement"],
basePath: new URL(data["dev"]["basePath"])
};
// set stagging by calling getter of ApiEnvConf class
apiEnvConf.stagingEnv = {
environnement: data["staging"]["environnement"],
basePath: new URL(data["staging"]["basePath"])
};
// set prod by calling getter of ApiEnvConf class
apiEnvConf.prodEnv = {
environnement: data["prod"]["environnement"],
basePath: new URL(data["prod"]["basePath"])
};
return apiEnvConf;
}
}

因此,如果您查看 loadEnvData() 函数,如果我在 return 之前创建一个 console.log(this.envParam); > 声明 我的控制台浏览器中有这个结果:

enter image description here

所以我的 ApiEnvConf 类不为空并且具有我需要的所有属性。

ImportConfig 类是我在许多文件中用来初始化环境配置的另一个类。

所以在另一个文件夹的另一个类中我有这个类:

import { Injectable, Component } from '@angular/core';
import { ImportConfig } from '../services/import-config';
import 'rxjs/add/operator/map';

@Injectable()
@Component({
providers: [ ImportConfig ]
})
export class Test {
constructor(public http: Http, public config: ImportConfig) {
this.config;
this.check();
}

check() {
let test = this.config.loadEnvData();
console.log(test);
}

/*
...
...
...
*/
}

console.log(test); 的输出返回给我 “undefined”。我哪里错了?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com