Angular:将参数传递给另一个组件

标签 angular typescript

我坚持使用 Angular2,我想将参数从 产品页面(例如:产品 ID)传递到 支付页面,这是我尝试过的到目前为止:

enter image description here

payment.html :

  Message: {{message}}
  <page-products></page-products>

payment.ts :

import { Component, ViewChild, AfterViewInit} from '@angular/core';
import { ProductsPage } from "../../products/products";

@IonicPage()
@Component({
    selector: 'page-payment',
    templateUrl: 'payment.html'
})
export class PaymentPage implements AfterViewInit {

     @ViewChild(ProductsPage) child;

     constructor(public navCtrl: NavController) {}

     message:string;

     ngAfterViewInit() {
             this.message = this.child.message
     }
    }

products.ts :

import { Component} from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';

@IonicPage()
@Component({
    selector: 'page-products',
    templateUrl: 'products.html',
})
 export class ProductsPage {

     message: string = "Hola Mundo!"

     constructor(public navCtrl: NavController) {}

     goToPayment() {
         this.navCtrl.setRoot('PaymentPage');
     }
 }

product.html :

<button ion-button (click)="goToPayment()">pay</button>

但我总是收到此错误消息:

enter image description here

知道我做错了什么吗?


here is my payment.module.ts:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ComponentsModule } from '../../../components/components.module';
import { PaymentPage } from './payment';
import { ProductsPage } from '../../products/products';

@NgModule({
   declarations: [
       PaymentPage, ProductsPage  <-- if I add here ProductsPage, then new error (see under)
   ],
   imports: [
        IonicPageModule.forChild(PaymentPage),
        ComponentsModule
   ]
})
export class PaymentPageModule {}

and products.module.ts :

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ProductsPage } from './products';
import { ComponentsModule } from '../../components/components.module';

@NgModule({
   declarations: [
      ProductsPage
   ],
   imports: [
      IonicPageModule.forChild(ProductsPage),
      ComponentsModule
  ]
})
export class ProductsPageModule {}

如果我像上面那样声明 ProductsPage,则会收到此错误消息:

enter image description here

最佳答案

要在没有层次关系的组件之间传递参数,您应该使用服务。为此,首先创建服务:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class MyService {

  private messageSource = new BehaviorSubject<type_of_the_message>(message);
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  sendMessage(message: type_of_the_message) {
    this.messageSource.next(message);
  }

}

然后在发送者组件中:

    import { Component, OnInit } from '@angular/core';
    import { MyService } from 'pathToService/MyService.service';

    @Component({
      selector: 'sender',
      templateUrl: 'senderTemplate',
      styleUrls: ['senderStyles']
    })
    export class SenderComponent implements OnInit {

      private message: type_of_the_message;

      constructor(private myService: MyService) {}

      ngOnInit() {
        this.myService.currentMessage.subscribe(message => this.message = message);
      }

      sendMessage() {
        this.myService.sendMessage(!this.message);
      }
    }

最后在接收组件中:

import { Component, OnInit } from '@angular/core';
import { MyService } from 'pathToService/MyService.service';

@Component({
  selector: 'receiver',
  templateUrl: 'receiverTemplate',
  styleUrls: ['receiverStyles']
})
export class ReceiverComponent implements OnInit {

  private message: boolean;

  constructor(private myService: MyService) {}

  ngOnInit() {
    this.myService.currentMessage.subscribe(message => this.message = message);
  }
}

关于Angular:将参数传递给另一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48763221/

相关文章:

angular - Angular 中的 ReactiveFormsModule 无法与 [formGroup] 一起使用(我到处都看过)

typescript - TypeScript 编译时如何检查 Omit?

reactjs - 如何使用 React 和 TypeScript 设置 Electron?

javascript - typescript :将方法添加到类的数组

javascript - 更改全屏 html5 视频的尺寸

用于打开侧面板的 CSS 在 Angular 组件中不起作用

typescript - 如何将回调定义为两种类型之一?

javascript - 使用 TypeScript react Children[] 问题

angular - 如何保证RESTful API只能被SPA应用使用?

angular - 如何使子页面的 ionic 标签可见?