在Typescript

时间:2018-06-09 01:26:10

标签: angular typescript

我想在前端将字符串拆分为json格式,但我的服务和组件之间的类型不匹配。

我接收的数组看起来像这样:

0:Active,1:Logged In,2:Password tries exceeded,3:Force password change,4:Expired,5:Disabled

我在控制台日志中找到了成功分离数组,但是当我想将它们设置为我的双矩阵数组时,它们不匹配或显示为未定义。

Service.ts

import 'rxjs/add/observable/from';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions } from '@angular/http';
import { UpdateGroup } from '../_models/updateGroup';


@Injectable()
export class StatusService {

    private splitList : String[];
    //parsedList : {id: String, name: String}[];
    private parsedList : String[][];
    constructor(private http: Http) {

    }

    parseList(toParse : String): String[][] {
        var fieldCount = Number.parseInt(toParse.substr(0, 1));
        toParse = toParse.substr(1);
        this.splitList = toParse.split(/(?::|,)/);
        console.log("Found string " + toParse);
        console.log("Field count is " + fieldCount);
        console.log("splitList length is " + this.splitList.length);

        for (let index = 0; index < this.splitList.length; index++) {
            console.log(this.splitList[index]);         
        }

        console.log("new length is " + this.splitList.length/fieldCount);
        this.parsedList = String[this.splitList.length/fieldCount][fieldCount]; //does not work


        for (let i = 0; i < this.splitList.length; i+=fieldCount) {
            for(let j = 0; j < fieldCount; j++)
                this.parsedList[i/fieldCount][j] = this.splitList[i+j];          
        }

        return this.parsedList;
      }
}

Component.ts

import { StatusService } from './../../../../_services/status.service';


@Component({
  selector: 'update-form',
  templateUrl: './updateForm.html',
})

export class UpdateFormComponent implements OnInit {

  public statusList: String[][];
  public groupList: String[][];

  constructor(private http: Http, fb: FormBuilder, protected service: UpdateUserService,
    private statusService: StatusService,
    protected router: Router,
    private _location: Location,
  ) {}


ngOnInit() {

    this.formService.myUser.filter(user => user !== null)
      .subscribe(user => {

        this.statusList = this.statusService.parseList(this.myUser.STATUS_LIST);
        this.groupList = this.statusService.parseList(this.myUser.GROUP_LIST);

      });


  }
}

错误即时获取与服务和组件有关,由于某种原因,它无法读取数组以在服务中设置它

  

错误类型错误:无法读取未定义的属性“2”       在StatusService.push ../ src / app / _services / status.service.ts.StatusService.parseList(status.service.ts:39)       在SafeSubscriber._next(updateForm.component.ts:162)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:253)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber.next(Subscriber.js:191)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._next(Subscriber.js:129)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js:93)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / filter.js.FilterSubscriber._next(filter.js:85)       在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js:93)       在BehaviorSubject.push ../ node_modules / rxjs / _esm5 / internal / Subject.js.Subject.next(Subject.js:53)       在BehaviorSubject.push ../ node_modules / rxjs / _esm5 / internal / BehaviorSubject.js.BehaviorSubject.next(BehaviorSubject.js:42)

1 个答案:

答案 0 :(得分:1)

在打字稿中,=用于分配,而不是指定类型。

当你这样做时:

this.parsedList = String[this.splitList.length/fieldCount][fieldCount]

那是试图为this.parsedList分配一个值,但它失败了,因为没有名为String的变量(因为它是一个类型,而不是一个值)。

相反,由于数组在js / typescript中是动态调整大小的,所以你可以像这样分配它:

this.parsedList = [];

这将创建一个1d数组。然后在你的循环中,你可以这样做:

for (let i = 0; i < this.splitList.length; i+=fieldCount) {
    this.parsedList[i/fieldCount] = []; // Assign an array to make it a 2d array
    for(let j = 0; j < fieldCount; j++)
        this.parsedList[i/fieldCount][j] = this.splitList[i+j];          
}