【问题标题】:angular 4 pass data between components without input\output角度 4 在没有输入\输出的组件之间传递数据
【发布时间】:2017-09-07 08:56:09
【问题描述】:

我正在尝试在角度 4 的组件之间传递数据。 我不想使用输入\输出。 在 Homecomponent 我想将数据推送到服务中,在 page2 组件中我想从服务中获取数据。 我只是看着 observable 和 subject 的方式,我试图实现它但没有成功。

主页组件

    import { ClientService } from './../clinet-service.service';
import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'page-home',
  template: 'pages/home/home.html',
})
export class HomeComponent implements OnInit {

  clients = ['john', 'jane']
  clientFound = false

  // constructor(private navController: NavController) { }
  constructor(private clientService: ClientService) {

  }

  openClient(client) {
  }
  ngOnInit() {
    this.clientService.subject.next([
      { name: 'Harold', age: 35 }
    ]
    );

}

}

page2组件

import { ClientService } from './../clinet-service.service';
import { Component } from '@angular/core';
@Component({
  selector: 'page-2',
  template: '{{clients}}'
})
export class Page2Component {
  client:any;

  constructor(  private clientService: ClientService) {

  }

  ngOnInit(){
    debugger
    let clients = this.clientService.observable.subscribe(clients => console.log(clients));
  }
}

客户服务

import { Injectable, OnInit, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from "rxjs/Observable";

@Injectable()

export class ClientService {
  subject: Subject<any> = new Subject<any>();
  observable: Observable<any> = this.subject.asObservable();


}

实际上我不喜欢这种实现方式,因为我尝试制作简单的东西,例如将数据推送到服务并在其他组件中获取它,但我做不到。

【问题讨论】:

  • 错误是什么
  • 我在 page2 中什么也没看到
  • 你没有在控制台中得到任何东西?
  • 是的,我什么也没得到
  • 当您在 ngOnInit 方法中从 Page2Component 中删除调试器行时会发生什么?

标签: angular angular2-components


【解决方案1】:

你不能这样做,因为你让它变得不必要的复杂。这是你应该做的:

主页组件

ngOnInit() {
    this.clientService.subject.next([{
      {name: 'Harold', age: 35 },
      {name: 'Kumar', age: 40 },
    }]);
}

page2Component

ngOnInit(){
  let clients = this.clientService.observable.subscribe(clients => console.log(clients));
}

服务

export class ClientService {

  subject: Subject<any> = new Subject<any>();
  observable: Observable<any> = this.subject.asObservable();

  constructor() {}
}

【讨论】:

  • 我尝试在父子项目中执行此操作,并且在父项之前加载子项,但它不起作用,所以我决定使用 observable 来执行此操作
  • 好吧,如果你在那之后使用 API(我猜你的服务被模拟了),你就不会有这个问题,因为你不会设置你的客户端,而只是获取它们。要检查这一点,您可以直接在服务中声明客户端变量的位置设置客户端。
  • 它有效,但这不是我想做的。我想从父组件获取数据,然后保存在服务中并与所有子组件共享。所有子组件都使用 get 函数在其组件中获取数据
  • 那么,取消模拟您的服务并编辑您的帖子。然后,我们将继续努力。因为如果您不这样做,我们将使用与实际 API 服务无关的概念。
  • @trichetriche 是什么意思?
【解决方案2】:

您可以使用 rxjs 主题在任何组件之间传递数据。像这样在您的服务中创建新的 rxjs 主题

import { Subject } from 'rxjs/Subject';
@Injectable()
export class MyService {

    myNewSubject = new Subject<any>();

    informDataChanges(passyourobject){
      this.myNewSubject.next(passyourobject);
  }

}

当您的组件发生更改或您想将数据传递给另一个组件时,只需从您的组件调用此服务函数并将数据作为参数传递给此函数。你可以用这样的东西来做到这一点

    import { Component, OnInit } from '@angular/core';

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

      constructor( private myService: MyService) { }

      someFunction(){
        this.myService.informLogout('somedata');//Passing data to service here
      }

  ngOnInit() {

  }

}

现在您需要做的就是在另一个组件中订阅该数据。重要的主题会持续关注它的任何变化,并且数据将是连续的流并且它会被自动订阅。所以最好在构造函数中订阅主题,更改会立即反映在该组件中。

你用这样的东西来做

import { Component, OnInit } from '@angular/core';

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

          constructor( private myService: MyService) {
            this.myService.myNewSubject.subscribe(data=>{
             console.log(data);
       }) 
}

这样您可以在任意数量的组件之间传递数据,并且任意数量的组件都可以使用相同的主题。

【讨论】:

    【解决方案3】:

    你的 HomeComponent 装饰器坏了。使用外部模板文件时,需要使用templateUrl 而不是template

    错误

    @Component({
      selector: 'page-home',
      template: 'pages/home/home.html',
    })
    

    正确

    @Component({
      selector: 'page-home',
      templateUrl: 'pages/home/home.html',
    })
    

    https://angular.io/api/core/Component

    【讨论】:

    • 我这样做是故意的,只是写了一个带有 url 的文本
    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 2020-09-12
    • 2019-08-27
    • 2021-12-29
    • 1970-01-01
    • 2017-11-20
    • 2020-01-14
    • 2019-09-25
    相关资源
    最近更新 更多