【发布时间】:2018-10-20 23:11:17
【问题描述】:
我正在尝试使这个示例 (https://angular.io/guide/set-document-title) 适应我的实现,它有更多的部分。我还没有完全理解所有有角度的 2 部分是什么以及它们如何组合在一起。
他们的 main.ts:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
我的:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
到目前为止还可以。
他们的 app.module:
import { NgModule } from '@angular/core';
import { BrowserModule, Title } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule
],
declarations: [
AppComponent
],
providers: [
Title
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的:
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { serviceConfigFactory, AppConfig } from './app.config';
... about 50 other imports
import { AppRoutingModule } from './app-routing.module';
export function HttpLoaderFactory(http: Http, siteService:SiteService) {
return new LocalizationLoader(http,siteService);
}
@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent,
BaseLoginComponent,
ClientSwitchComponent,
SwitchClientComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JWBootstrapSwitchModule,
DatepickerModule.forRoot(),
TypeaheadModule.forRoot(),
MomentModule,
SharedModule,
AuthModule,
CompanyModule,
RequestTimeOffModule,
AppRoutingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [Http, SiteService]
}
}),
StoreModule.provideStore({ appState: appStateReducer }),
],
providers: [
{ provide: AppConfig, useFactory: serviceConfigFactory },
SiteService,
AppContext,
{ provide: APP_INITIALIZER, useFactory: AppContextLoader, deps: [AppContext], multi: true },
EmployeeService,
LeavesService,
ServiceManager,
EncryptionService
],
bootstrap: [AppComponent]
})
export class AppModule { }
这就是我遇到麻烦的地方:
他们的 app.component.ts:
import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'my-app',
template:
`<p>
Select a title to set on the current HTML document:
</p>
<ul>
<li><a (click)="setTitle( 'Good morning!' )">Good morning</a>.</li>
<li><a (click)="setTitle( 'Good afternoon!' )">Good afternoon</a>.</li>
<li><a (click)="setTitle( 'Good evening!' )">Good evening</a>.</li>
</ul>
`
})
export class AppComponent {
public constructor(private titleService: Title ) { }
public setTitle( newTitle: string) {
this.titleService.setTitle( newTitle );
}
}
我的:
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { AppContext } from './services/app.context';
import {AuthService} from './services/auth/auth.service';
import { CompanyRoutes } from './features/company/company.routes';
import { Router, NavigationEnd } from '@angular/router';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(public router: Router, public auth: AuthService, public appContext: AppContext, translate: TranslateService, private companyRoutes: CompanyRoutes, private titleService: Title) {
if (appContext.siteConfig.clientCode) {
this.companyRoutes.resetCompanyRoutes(appContext.siteConfig.clientCode);
}
}
public setTitle(newTitle: string) {
this.titleService.setTitle(newTitle);
}
}
现在,我不希望它在 app.component .ts 上执行 setTitle;我想在它到达第一页时立即执行此操作(然后是每个后续页面,所以这是我期望做的:
我的 login.component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { SiteService } from '../../../services/site.service';
import { AppContext } from '../../../services/app.context';
import { LoginConfig, AppState } from '../../../models/site/site.config';
import {AuthService} from '../../../services/auth/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class BaseLoginComponent implements OnInit {
ngOnInit() {
this.setTitle("!");
}
}
login.component.ts 无法识别 this.setTitle() 或 setTitle();
我不太了解这些 app.component 和 app.module 字段的用途,以及它们如何连接在一起,所以我不确定如何将 setTitle 公开到我的页面。
【问题讨论】:
-
你离开了 setTitle(newTitle: string) { this.titleService.setTitle(newTitle); } 在 app.component.ts 中。你必须把它放在 BaseLoginComponent 中。并且不要忘记移动 TitleService 的导入
-
是的,这就是我想知道的。我要把它放在每一页上??
-
我不确定你是否理解。您是在问是否应该在每个页面上放置 setTitle 以使您的应用始终保持相同的标题?
-
啊。对不起。每个页面的内容都包含在一个通用的 _IndexLayout.html 中。所以每个页面都需要告诉标题标签它的名字是什么。 (这是一个通常通过路由处理的参数。)
-
当你调用 this.doSomething() 时,doSomething 是一个应该定义在同一个类中的方法,因为 'this' 指的是类。如果你需要一个服务中的方法,你可以通过构造函数注入它,你可以像这样调用 someService.doSomething()。
标签: angular