typescript 无法访问对象属性

efzxgjgh  于 2022-12-14  发布在  TypeScript
关注(0)|答案(1)|浏览(169)

场景我正在从journeys-list组件向mat-dialog注入数据。数据(journey对象)在dialog组件中正确传递和接收。但是当我尝试访问它的一个属性时,该属性似乎未定义,而我的journey对象确实包含所有值。如x1c 0d1x所示
旅程列表.组件.ts

import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { DialogComponent } from '../dialog/dialog.component';

@Component({
  selector: 'app-journeys-list',
  templateUrl: './journeys-list.component.html',
  styleUrls: ['./journeys-list.component.scss']
})
export class JourneysListComponent implements OnInit {

  constructor(private router: Router,
    private dialog: MatDialog) { }
  journeysObject: any;

  ngOnInit(): void {
    this.journeysObject = history.state;
    console.log(this.journeysObject);
  }

  parseTime(timestamp: any): any {
    const date = new Date(timestamp);
    return date.getHours() + ':' + date.getMinutes();
  }
  openDialog(journey: any) {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = true;
    dialogConfig.width= '50%';
    dialogConfig.height= '50%';
    dialogConfig.data ={journey};
    this.dialog.open(DialogComponent, dialogConfig);
  }
}

对话框组件.ts

import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss']
})
export class DialogComponent implements OnInit {

  journey:any={};
  constructor(
    public dialogRef: MatDialogRef<DialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any) {
      this.journey = data;
      const type = this.journey.type; //  undefined
  }
  ngOnInit(): void {
  }
  
  parseTime(timestamp: any): any {
    const date = new Date(timestamp);
    return date.getHours() + ':' + date.getMinutes();
  }

  close() {
    this.dialogRef.close();
  }
}

问题我的问题是

1.对象是否正确注入到对话框组件中?
1.是因为我没有为对象定义正确的类型而导致的Typescript问题吗?

ocebsuys

ocebsuys1#

删除此行的花括号。

dialogConfig.data = journey;

相关问题