Angular中的动态标题
创始人
2024-10-31 04:30:13
0
  1. 在AppComponent中定义一个属性来存储页面标题,例如可以定义为title: string。
  2. 在AppComponent中使用Title服务来设置页面标题。在构造函数中注入Title服务,然后在ngOnInit中调用setTitle方法设置页面标题。 示例代码如下:

import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {

title: string = 'My App';

constructor(private titleService: Title) {}

ngOnInit() { this.titleService.setTitle(this.title); } }

  1. 在需要动态修改页面标题的组件中,修改title属性的值即可。 示例代码如下:

import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser';

@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit {

constructor(private titleService: Title) { }

ngOnInit() { this.titleService.setTitle('Home | My App'); }

}

注意:页面标题的设置需要在Angular的Zone之外执行。相应地,Title服务会自动执行NgZone.run()。因此,可以在页面标题的设置中不需要再手动调用NgZone。

相关内容

热门资讯

曝光透视!哈糖大菠萝怎么挂!本... 曝光透视!哈糖大菠萝怎么挂!本来真的有辅助插件(竟然有挂)-哔哩哔哩1、超多福利:超高返利,海量正版...
专业透视!pokerworld... 专业透视!pokerworld破解版下载!真是一直总是有辅助软件(有挂秘诀)-哔哩哔哩1、让任何用户...
解密透视!德州真人透视脚本!果... 解密透视!德州真人透视脚本!果然有辅助app(有挂细节)-哔哩哔哩1、任何德州真人透视脚本透视是真的...
辅助透视!hardrock透视... 辅助透视!hardrock透视工具!其实真的是有辅助脚本(有挂秘笈)-哔哩哔哩1、金币登录送、破产送...
解密透视!pokermaste... 解密透视!pokermaster辅助器!果然真的是有辅助工具(有挂讲解)-哔哩哔哩1、pokerma...
必备透视!拱趴大菠萝有什么挂!... 必备透视!拱趴大菠萝有什么挂!果然真的是有辅助工具(有挂透视)-哔哩哔哩1、首先打开拱趴大菠萝有什么...
推荐透视!约局吧德州透视!确实... 推荐透视!约局吧德州透视!确实一直都是有辅助攻略(的确有挂)-哔哩哔哩1、约局吧德州透视辅助软件下载...
必备透视!拱趴大菠萝有什么挂!... 必备透视!拱趴大菠萝有什么挂!一直一直总是有辅助软件(有挂方式)-哔哩哔哩1)拱趴大菠萝有什么挂辅助...
科普透视!xpoker辅助工具... 科普透视!xpoker辅助工具!一贯是真的有辅助神器(确实有挂)-哔哩哔哩一、xpoker辅助工具游...
科普透视!德州真人透视脚本!本... 科普透视!德州真人透视脚本!本来存在有辅助技巧(有挂攻略)-哔哩哔哩德州真人透视脚本能透视中分为三种...