我在函数中定义了一个属性
evs: string
...
openArticle(url){
this.evs = url
console.log(this.evs)
this.navCtrl.navigateForward('/url-page')
}
[我试图将'this.evs'的值传递给另一个ts文件并使用它的值,但是我不知道该怎么做。我尝试过这样导出它。
export const webpage = this.evs
但是在有人执行openArticle函数广告之前,this.evs没有任何价值,因此我不断收到错误消息。 “无法读取未定义的属性'evs'”
我需要做的是仅在调用openArticle函数之后,才将变量转移到'url-page'页面并使用this.evs的值。我该怎么办?
根据我的理解,您正在尝试在两个组件之间共享数据。因此,请根据您的要求选择其中之一。
父级:通过Input()共享数据。
孩子到父母:通过Output()和EventEmitter共享数据。
不相关的组件:与服务共享数据。
使用@Input()从父级到子级共享数据:
<h3>Parent Component</h3>
<label>Parent Component</label>c
<input type="number" [(ngModel)]='parentValue'/>
<p>Value of child component is: </p>
<app-child [value]='parentValue'></app-child>
并且在子组件中,'parentValue'可以作为::>接收。
import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnInit { @Input() value: number; constructor() { } ngOnInit() { } }
现在,在将数据从子级发送到父级的情况下,我们可以使用@Output()事件发射器。因此,父级将具有以下功能::>>
parent-app.component.html <app-child [value]="parentValue" (childEvent)="childEvent($event)"></app-child> parent-app.component.ts childEvent(event) { console.log(event); } And, the child.component.ts would look like : import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnInit { @Input() PData: number; @Output() childEvent = new EventEmitter(); constructor() { } onChange(value) { this.childEvent.emit(value); } ngOnInit() { } }
- 如果组件不具有父/子关系,则可以使用共享服务,例如具有BehavioralSubject的SharedService,该服务从任一组件发出值,然后另一个组件可以捕获更改的值。
例如:
import { Injectable } from '@angular/core'; import { BehaviorSubject } from "rxjs/BehaviorSubject"; @Injectable() export class SharedService { comp1Val: string; _comp1ValueBS = new BehaviorSubject<string>(''); comp2Val: string; _comp2ValueBS = new BehaviorSubject<string>(''); constructor() { this.comp1Val; this.comp2Val; this._comp1ValueBS.next(this.comp1Val); this._comp2ValueBS.next(this.comp2Val); } updateComp1Val(val) { this.comp1Val = val; this._comp1ValueBS.next(this.comp1Val); } updateComp2Val(val) { this.comp2Val = val; this._comp2ValueBS.next(this.comp2Val); }
和component1如下:
import { Injectable } from '@angular/core'; import { BehaviorSubject } from "rxjs/BehaviorSubject"; @Injectable() export class SharedService { comp1Val: string; _comp1ValueBS = new BehaviorSubject<string>(''); comp2Val: string; _comp2ValueBS = new BehaviorSubject<string>(''); constructor() { this.comp1Val; this.comp2Val; this._comp1ValueBS.next(this.comp1Val); this._comp2ValueBS.next(this.comp2Val); } updateComp1Val(val) { this.comp1Val = val; this._comp1ValueBS.next(this.comp1Val); } updateComp2Val(val) { this.comp2Val = val; this._comp2ValueBS.next(this.comp2Val); }
Component 2:
import { Component, AfterContentChecked } from '@angular/core'; import { SharedService } from "../../common/shared.service"; @Component({ selector: 'app-component2', templateUrl: './component2.component.html', styleUrls: ['./component2.component.css'] }) export class Component2Component implements AfterContentChecked { comp1Val: string; comp2Val: string; constructor(private sharedService: SharedService) { this.sharedService.comp2Val = "Component 2 initial value"; } ngAfterContentChecked() { this.comp1Val = this.sharedService.comp1Val; } addValue(str) { this.sharedService.updateComp2Val(str); } }
您可以找到有关不同类型主题的更多信息here