Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 加载页面时如何从不同组件中的数组中获取数据_Angular_Typescript - Fatal编程技术网

Angular 加载页面时如何从不同组件中的数组中获取数据

Angular 加载页面时如何从不同组件中的数组中获取数据,angular,typescript,Angular,Typescript,我在组件B中有一些数组,其中有些链接如下: videoUrlArray = [ 'link1', 'link2',... ]; constructor() { this.videoUrlArray.push('someExampleLink3'); } addCategoryBonStart(): void { this.videoUrl.push(this.BObject); this.sanitizedVideos = this.v

我在组件B中有一些数组,其中有些链接如下:

videoUrlArray = [
    'link1', 'link2',...
  ];
  constructor() {
    this.videoUrlArray.push('someExampleLink3');
  }
  addCategoryBonStart(): void {
      this.videoUrl.push(this.BObject);
      this.sanitizedVideos = this.videoUrl.map(video => this.sanitizer.bypassSecurityTrustResourceUrl(video));
    console.log('BObject' + this.BObject);
  }
通常,我使用ViewChild从一个组件到另一个组件获取数据,它工作正常,但如果单击按钮,我需要数据。现在我需要数据总是在组件A中,它来自B

我想出了将数据从B组件传递到over构造函数的主意(比如在Java中-我是使用TS学习Java的本地人)

B中的构造函数如下所示:

videoUrlArray = [
    'link1', 'link2',...
  ];
  constructor() {
    this.videoUrlArray.push('someExampleLink3');
  }
  addCategoryBonStart(): void {
      this.videoUrl.push(this.BObject);
      this.sanitizedVideos = this.videoUrl.map(video => this.sanitizer.bypassSecurityTrustResourceUrl(video));
    console.log('BObject' + this.BObject);
  }
并期望通过以下方式获取组件中的数据:

  @Inject(new BComponent()) BObject;
我希望这样的方法:

videoUrlArray = [
    'link1', 'link2',...
  ];
  constructor() {
    this.videoUrlArray.push('someExampleLink3');
  }
  addCategoryBonStart(): void {
      this.videoUrl.push(this.BObject);
      this.sanitizedVideos = this.videoUrl.map(video => this.sanitizer.bypassSecurityTrustResourceUrl(video));
    console.log('BObject' + this.BObject);
  }
在ngOnInit中实施将完成他们的工作。与此相反,我得到了一些iframe的错误,这些iframe未定义,看起来很糟糕:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'undefined'
Error: Cannot match any routes. URL Segment: 'undefined'
为什么它没有定义?我正在用B对象的构造函数创建数组元素? 如何在加载页面时获取对象


在html模板中,一个组件(我试图从B中的数组接收数据)被添加到B模板中,因此它应该可以工作。

据我所知,我建议使用一个共享服务,这两个组件都可以导入

此外,我建议使用行为主题。这些可以由共享服务的公共方法设置,并由另一个组件订阅

组件A可以通过setter方法推送元素,组件B可以订阅行为主题

我建议阅读这个主题,因为它在我看来是一个非常强大的概念