[英]Angular: Getter/Setter - Getter is returning undefined
I'm trying to pass a variable that is set on a component, to the parent component via a getter/setter in a service. 我正在尝试通过服务中的getter / setter将在组件上设置的变量传递给父组件。 The setter is applied correctly, but the getter returns undefined. 正确使用了setter,但是getter返回的未定义。
The below code was pulled out of another project I work on that does just fine with this code so I'm not sure why it isn't working here. 下面的代码是从我正在处理的另一个项目中拉出来的,该代码对这个代码也很好,所以我不确定为什么它在这里不起作用。
I simply just need to pass the pageTitle that is set on the child component, pass it to the parent component to display in its HTML. 我只需要传递在子组件上设置的pageTitle,然后将其传递给父组件以在其HTML中显示。
Parent Component 父组件
TS: styleguide.component.ts TS:styleguide.component.ts
import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { StyleguideService } from './styleguide.service';
@Component({
selector: 'styleguide',
templateUrl: './styleguide.component.html',
host: {'class': 'route'},
})
export class StyleguideComponent {
constructor( private ss: StyleguideService ) {}
}
Relevant HTML: styleguide.component.html 相关HTML:styleguide.component.html
<a [routerLink]="[]" aria-current="page" class="crumbs__link crumbs__link--active" [title]="ss.pageTitle">{{ss.pageTitle}}</a>
Parent Module: styleguide.module.ts 父模块: styleguide.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { StyleguideService } from './styleguide.service';
import { StyleguideComponent } from './styleguide.component';
import { TemplatesComponent } from './templates/templates.component';
...
@NgModule({
imports: [
CommonModule,
FormsModule,
...
],
declarations: [
StyleguideComponent,
TemplatesComponent,
...
],
exports: [
...
],
providers: [
StyleguideService
]
})
export class StyleguideModule {}
Service: styleguide.service.ts 服务: styleguide.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class StyleguideService {
pageTitleS: string;
get pageTitle(): string {
console.log('get title: ', this.pageTitleS); // <-- Returns undefined
return this.pageTitleS;
}
set pageTitle(s: string) {
console.log('set title: ', s);
this.pageTitleS= s;
}
}
Child Component: templates.component.ts 子组件: templates.component.ts
import { Component } from '@angular/core';
import { StyleguideService } from '../styleguide.service';
@Component({
selector: 'templates',
templateUrl: './templates.component.html',
host: {'class': 'route__content'}
})
export class TemplatesComponent {
constructor( private ss: StyleguideService ) {
this.ss.pageTitle = "Templates";
}
}
Your setter is never called. 您的二传手永远不会被叫。 You instantiate the service using StyleguideComponent
, not the TemplatesComponent
which does call the setter, and the constructor of StyleguideComponent
does not call the setter on the service which is why the value remains undefined. 您可以使用StyleguideComponent
实例化服务,而不是使用调用设置程序的TemplatesComponent
实例化,并且StyleguideComponent
的构造StyleguideComponent
不会在服务上调用设置程序,这就是为什么值保持不确定的原因。
The TemplatesComponent
has an element selector templates
which I do not see in the styleguide.component.html
you have in the question which is why I believe TemplatesComponent
is never being created. TemplatesComponent
有一个元素选择器templates
,在您遇到的问题中,我没有在styleguide.component.html
看到它,这就是为什么我认为从未创建过TemplatesComponent
原因。
You are not calling the setter function in your child.component.ts instead you are setting the value of variable but I think you are accessing it wrongly as you are missing the last S
in the variable name. 您不是在child.component.ts中调用setter函数,而是在设置变量的值,但由于丢失了变量名中的最后一个S
,因此我认为访问错误。 You should be doing 你应该在做
export class TemplatesComponent {
constructor( private ss: StyleguideService ) {
this.ss.pageTitle("Templates");
// Now to get it you should call
this.ss.pageTitle(); // Should console.log the value
}
}
You should implement the Service with Observables. 您应该使用Observables实现服务。 A quick example would be something like this: 一个简单的例子就是这样:
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Injectable} from '@angular/core'
@Injectable()
export class Service {
private value: BehaviorSubject<string>;
constructor() {
this.value = <BehaviorSubject<string>>new BehaviorSubject();
}
setValue(value=""){
this.value.next(value);
}
getValue() {
return this.value.asObservable();
}
}
The Parent Component would subscribe to it like so: 父组件将订阅它,如下所示:
import {Component, OnInit} from '@angular/core'
import { Service } from './service';
@Component({
selector: 'parent-component',
template: `
<div>
<h2>Value {{value}}</h2>
<child-component></child-component>
</div>
`,
})
export class ParentComponent implements OnInit {
value:string;
constructor(private service: Service) {
}
ngOnInit(){
this.service.getValue().subscribe((newValue)=>{
this.value = newValue;
})
}
}
And the Child Component would set the value and also subscribe to it like so: 子组件将设置该值并订阅它,如下所示:
import {Component, OnInit} from '@angular/core'
import { Service } from './service';
@Component({
selector: 'child-component',
template: `
<div>
<h2>Child Value {{value}}</h2>
</div>
`,
})
export class ChildComponent implements OnInit {
value:string;
constructor(private service: Service) {
this.service.setValue('New Value');
}
ngOnInit(){
this.service.getValue().subscribe((newValue)=>{
this.value = newValue;
})
}
}
好吧,这与我的路由设置有关,我没有正确设置我的子路由,因此这毕竟与getter / setter无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.