Angular小博客

分享让你更聪明

Angular怎么修改title内容

浏览:81次 日期:2024年08月20日 8:45:06 作者:admin

很多时候每一个页面都需要不同的Title,下面是Angular修改浏览器title的两种方法。

路由中设置title

import {Routes} from '@angular/router';

export const routes: Routes = [
    {path: '', loadComponent: () => import('./pages/index/index.component').then(m => m.IndexComponent), title: '首页'},
    {path: 'list', loadComponent: () => import('./pages/list/list.component').then(m => m.ListComponent), title: '列表'},
    {path: 'details', loadComponent: () => import('./pages/details/details.component').then(m => m.DetailsComponent), title: '详情'},
];

组件中修改title

import {Title} from "@angular/platform-browser";
export class IndexComponent implements OnInit {
    constructor(private title: Title) {}
    ngOnInit(): void {
        setInterval(() => {
            this.title.setTitle('博客www.ng1024.com');
        }, 2000)
    }
}