角度:将参数传递给另一个组件 [英] Angular: pass parameter to another component

查看:50
本文介绍了角度:将参数传递给另一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm stuck with Angular2, I would like to pass parameters from a Products page (eg: product ids) to a Payment page, here is what I tried so far:

payment.html:

payment.html :

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

payment.ts:

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:

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:

product.html :

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

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

but I always get this error message:

有什么想法我做错了吗?

Any idea of what I'm doing wrong?

这是我的payment.module.ts:

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 {}

和products.module.ts:

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,则会收到此错误消息:

and if I declare ProductsPage like above then I get this error message:

推荐答案

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

To pass parameters between components that dont have a hierarchical relationship you should use a service. To achieve this, first create the service:

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);
  }
}

这篇关于角度:将参数传递给另一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆