gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:05:56 25 4
gpt4 key购买 nike

我坚持使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com