代码之家  ›  专栏  ›  技术社区  ›  user1828605

为什么我在将数据推送到角度为10的数组时总是出错?

  •  0
  • user1828605  · 技术社区  · 4 年前

    enter image description here

    这是组件

    import { Component, Input, OnInit } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { IStatement } from 'src/Interface/ICorporateStatement';
    import { StatementService } from '../services/statement.service';
    import { MatChipInputEvent } from '@angular/material/chips';
    import {COMMA, ENTER} from '@angular/cdk/keycodes';
    
    export interface Tag{
      corporate_statement_link_id: number;
      name: string;
    }
    
    
    @Component({
      selector: 'app-statement-detail',
      templateUrl: './statement-detail.component.html',
      styleUrls: ['./statement-detail.component.css']
    })
    export class StatementDetailComponent implements OnInit {
    
      statement: IStatement;
      id: number;
    
      tags: Tag[] = [];
      
      visible = true;
      selectable = true;
      removable = true;
      addOnBlur = true;
      readonly separatorKeysCodes: number[] = [ENTER, COMMA];
      
      constructor(private statementService: StatementService, 
        private router:ActivatedRoute) { }
    
      ngOnInit(): void {
    
        this.tags = [
          { corporate_statement_link_id: 1, name: 'EDI'}
        ];
      
        console.log("Tags: ", this.tags);
    
        this.router.queryParams.subscribe(param => {
          this.id = param.id;
          this.getStatement(this.id);
        });
      }
    
      addTag(event: MatChipInputEvent): void {
    
        console.log(this.tags);
        const input = event.input;
        const value = event.value;
        console.log("Input: ", input);
        console.log("Value: ", value);
        console.log("Tags: ", this.tags);
    
        this.tags.push({corporate_statement_link_id: this.statement.corporate_statement_link_id, name: value.trim()});
    
        // // Add our fruit
        // if ((value || '').trim()) {
        //   this.fruits.push({name: value.trim()});
        // }
    
        // // Reset the input value
        // if (input) {
        //   input.value = '';
        // }
      }
    
      removeTag(tag: Tag): void {
        console.log("removing");
        // const index = this.fruits.indexOf(fruit);
    
        // if (index >= 0) {
        //   this.fruits.splice(index, 1);
        // }
      }
      // get statement
      getStatement(id){
        this.statementService.getStatement(id).subscribe(data => {
          this.statement = <IStatement>data[0];
    
          //get tags
          this.statementService.getTags(this.statement.corporate_statement_link_id)
            .subscribe(tag => {
              this.tags = <Tag[]>tag;
            })
    
        }, error => {
          console.log(error);
        });
    
      }
    
    }
    

    我已经重构了代码,并在这里和那里移动了一些东西,但是仍然不能理解为什么数组仍然是空的。

    1 回复  |  直到 4 年前
        1
  •  1
  •   JBoothUA    4 年前

    看起来这个代码正在设置这个.tags为空。

    .subscribe(tag => {
              this.tags = <Tag[]>tag;
            })
    

    如果需要,可以用如下空数组替换此处的任何空值:

    .subscribe(tag => {
              this.tags = <Tag[]>tag || [];
            })
    

    看看有没有帮助。