[英]Angular 5 Title service is undefined
I try to change the page title with BrowserModule. 我尝试使用BrowserModule更改页面标题。 I added the BrowserModule and Title in application module, like here: https://angular.io/guide/set-document-title 我在应用程序模块中添加了BrowserModule和Title,例如: https : //angular.io/guide/set-document-title
In a child module (I tried to add here the service and module (BrowserModule) too) I have a component where I insert the Title service, but that service is 'undefined'. 在子模块中(我也尝试在此处添加服务和模块(BrowserModule)),我有一个组件在其中插入Title服务,但是该服务是“未定义的”。
module 模
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { RouterModule, Routes } from "@angular/router";
import { ProductService } from "../../services/Product";
import { ProductComponent } from "../../components/product/component";
import { ProductResolve } from "../../components/product/resolve";
const routes: Routes =
[
{
path: "produs/:url",
component: ProductComponent,
resolve:
{
Product: ProductResolve,
},
},
];
@NgModule({
imports:
[
CommonModule,
RouterModule.forChild(routes),
],
providers:
[
ProductResolve,
ProductService,
],
declarations:
[
ProductComponent,
],
exports:
[
RouterModule,
ProductComponent,
],
})
export class ProductModule { }
component: 零件:
import { Component, OnInit, OnDestroy } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { Product } from "../../services/Product";
import { Title } from '@angular/platform-browser';
@Component({
templateUrl: "../../templates/product/component.html",
styleUrls: [
"../../sass/product/component.scss",
]
})
export class ProductComponent implements OnInit, OnDestroy
{
private Subscribe: any;
constructor(private titleService: Title, private Route: ActivatedRoute)
{
}
ngOnInit()
{
this.Subscribe = this.Route.data.subscribe(this.process);
}
private process(product: Product)
{
//console.log(this.title);
//this.titleService.setTitle(product.Title);
}
ngOnDestroy()
{
this.Subscribe.unsubscribe();
}
}
app module 应用模块
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
...
@NgModule({
declarations: [
...
],
imports: [
...
BrowserModule,
],
providers: [LoadingScreenService, Title],
bootstrap: [AppComponent],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }
The problem is a common one: when a class method is used as a callback function, this
is incorrect inside of the method. 这个问题很常见:当将类方法用作回调函数时, this
在方法内部是不正确的。 You should use an arrow function as the callback: 您应该使用箭头函数作为回调:
this.Subscribe = this.Route.data.subscribe((product: Product) => {
this.process(product);
});
Seems like you are losing the scope. 好像您正在失去范围。 In the process()
scope this
doesn't represent your class instance anymore. 在process()
范围this
已不表示你的类的实例。
Adding .bind(this)
should solve your problem. 添加.bind(this)
应该可以解决您的问题。
this.Subscribe = this.Route.data.subscribe(this.process.bind(this));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.