简体   繁体   English

Angular 6-ngOnInit中的函数不会触发

[英]Angular 6 - functions in ngOnInit won't trigger

My problem is that in setStyle() function, I have the right values for those 2 arrays, but it won't enter on .map . 我的问题是在setStyle()函数中,我拥有这2个数组的正确值,但不会在.map输入。 Why is that? 这是为什么? Where should I call setStyle() function in order to trigger .map , if not in ngOnInit ? 如果不在ngOnInit ,应该在哪里调用setStyle()函数以触发.map

  ngOnInit() {
      this.existingCustomers = this.trackService.refreshExCusts()
      this.nonExistingCustomers = this.trackService.refreshNonExCusts()
      this.setStyle()
  }

  setStyle() {
    // it enters here
    console.log(this.existingCustomers) // has the right value
    console.log(this.nonExistingCustomers) // has the right value

    this.existingCustomers.map((val) => this.lineStyle.push({ // won't enter here
        "customer": val.customer_name,
        "color": '#000000'
    }))

    this.nonExistingCustomers.map((val) =>  this.lineStyle.push({ // won't enter here
      "customer": val.customer_name,
      "color": '#ff0000'
    }))

    console.log(this.lineStyle) // this is empty
  }

The value of the arrays: 数组的值:

existingCustomers = [{customer_name: "a"}, {customer_name: "b"}]

nonExistingCustomers = [{customer_name: "c"}, {customer_name: "d"}]

Thank you for your time! 感谢您的时间!

The object arrays are defined incorrectly, they should look like below for your code to work: 对象数组定义不正确,它们应类似于下面的代码,以便工作:

  existingCustomers = [{ customer_name: "a" }, { customer_name: "b" }];

  nonExistingCustomers = [{ customer_name: "c" }, { customer_name: "d" }];

I tested with these arrays, and I get the lineStyle printed in console as: 我测试了这些数组,并在控制台中将lineStyle打印为:

[{"customer":"a","color":"#000000"},{"customer":"b","color":"#000000"},{"customer":"c","color":"#ff0000"},{"customer":"d","color":"#ff0000"}]

UPDATE : OP Confirmed that the Array definition was typo in the question. 更新 :OP确认问题中的数组定义是拼写错误。 So my only other suspect is how the map or forEach take a callback function and therefore race condition when console.log prints the array. 因此,我唯一的另一个怀疑是mapforEach如何使用回调函数,并因此在console.log打印数组时出现竞争状态。 Try the for loops: 尝试for循环:

  setStyle() {
    // it enters here
    console.log(this.existingCustomers) // has the right value
    console.log(this.nonExistingCustomers) // has the right value

    for (let i = 0; i < this.existingCustomers.length; i++) {
      this.lineStyle.push({ // won't enter here
        "customer": this.existingCustomers[i].customer_name,
        "color": '#000000'
      })
    }

    for (let i = 0; i < this.nonExistingCustomers.length; i++) {
      this.lineStyle.push({ // won't enter here
        "customer": this.nonExistingCustomers[i].customer_name,
        "color": '#ff0000'
      })
    }

    console.log(JSON.stringify(this.lineStyle)) // this is empty
  }

Make sure that you are implementing onInit: 确保您正在实现onInit:

import { OnInit } from '@angular/core';
export class Random implements OnInit {
  constructor() { }

  // implement OnInit's `ngOnInit` method
  ngOnInit() { 
     console.log(`OnInit`); 
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM