【问题标题】:Page titles with router event returns same title in Angular2带有路由器事件的页面标题在 Angular2 中返回相同的标题
【发布时间】:2019-03-14 12:27:57
【问题描述】:

我使用来自AppComponent 的路由器事件跟踪路线并更改标题,如下所示。 应用路由模块

  { path: 'Home', component: ErrorComponent, data: {  title: 'Home Page' } },
  { path: 'Login', component: LoginComponent, data: {  title: 'Login' } },
  { path: 'ForgotPassword', component: ForgotComponent, data: {  title: 'Forgot Password' } },

  { path: 'Register', component: RegisterComponent, data: {  title: 'New Registration' } },
  { path: 'Register/:id', component: RegisterComponent, data: {  title: 'Update Profile' } }

在这里,如果我导航到 Home、Login、ForgotPassword 之间,一切正常,但是当导航到 Register 一次时,然后导航到任何其他页面,它会为我提供所有进一步导航的 Register Page 标题。

应用组件

constructor(private router: Router,
    private activatedRoute: ActivatedRoute,
    private titleService: Title) {
    this.router.events.pipe(
      filter((event) => event instanceof NavigationEnd),
      map(() => this.activatedRoute),
      map((route: any) => {
        while (route.firstChild) route = route.firstChild;
        return route;
      }),
      filter((route) => route.outlet === 'primary'),
      mergeMap((route: any) => route.data)).subscribe((event) => {
        this.titleService.setTitle(event['title']);
        console.log('Page Title', event['title']);
      });
  }

我在这里缺少什么?我正在使用 Angular 7。

【问题讨论】:

  • 请您创建一个stackblitz.com 小提琴,好吗?
  • 控制台是否还记录“注册页面的标题”或是否记录正确的标题?
  • 我为我创建了一个小提琴,它可以工作,请参阅stackblitz.com/edit/angular-title-by-routing
  • 我也刚刚创建了一个小提琴并且工作正常stackblitz.com/edit/angular-4basu6 但不幸的是没有在我的实际项目中工作。我认为我还缺少其他一些东西。
  • 您可以尝试使用this.router.routerState.root 代替this.activatedRoute,有帮助吗?

标签: angular angular2-routing angular7


【解决方案1】:

我建议您可以使用以下方法在每次路线更改时使用动态标题

这对我来说很好用

首先创建新的.ts file,命名为

page-titles.ts

root folderconstants文件夹中(或任何你想要的地方)

export const DETAILS = {
    HOME_PAGE: {
        title: 'Millions of reviews analyzed & ranked',
        meta: 'cc analyzes all reviews from all review sites and shows you what other consumers find important.',
    },
    SEARCH_REVIEW: {
        title: 'Search Results -test.com',
        meta: '',
    },
    TERMS: {
        title: 'Terms and Conditions',
        meta: '',
    },
    BLOGS: {
        title: 'The Consumerchoice Blog',
        meta: '',
    },
    BLOG_DETAILS: {
        title: 'Blog post title',
        meta: '',
    },
    PRESSES: {
        title: 'Consumerchoice in the Press',
        meta: '',
    },
    PRESS_DETAILS: {
        title: 'Press post title test',
        meta: '',
    },
    ABOUT: {
        title: 'Learn about Consumerchoice and our team',
        meta: '',
    },
    CONTACT: {
        title: 'Contact test.com',
        meta: '',
    },
    FAQS: {
        title: 'Questions and Answers',
        meta: '',
    },
    CATEGORY: {
        title: 'All test.com Categories',
        meta: '',
    },
    SERVICE_DETAILS: {
        title: 'Home | Service Details',
        meta: '',
    }
}

此文件将在此处的主常量对象中具有键对值,即DETAILS 其中DETAILS 对象的每个键代表页面名称(与特定路由关联的组件名称)因此,您无需在路由中设置数据属性

然后在您的特定路由组件中只需使用seTitle 方法来设置标题

例如,我想为Terms Page 设置标题,然后将此代码添加到该组件的构造函数中

terms.component.ts

import { DETAILS } from '../../constants/page-details';


constructor(public titleService: Title,) {

   this.titleService.setTitle(DETAILS.TERMS.title);

}

如果这对你有帮助,请告诉我!!

只需尝试使用以下代码更新app.component.ts 的构造函数

constructor(
    public router: Router,
    public activatedRoute: ActivatedRoute,
    public titleService: Title
  ) {
    router.events
      .filter(e => e instanceof NavigationEnd)
      .subscribe((event) => {
        console.log('title from route data :', 
         activatedRoute.root.firstChild.snapshot.data['title']);
        this.title = 
         activatedRoute.root.firstChild.snapshot.data['title'];
        this.titleService.setTitle(this.title);
      })
     }

【讨论】:

  • 是的,这是设置标题的一种方法,但这里的问题是我们需要更新每个组件的 ts 文件,而使用路由器事件,只需添加一个 sn-p AppComponent 就足够了。将等待一段时间,如果没有答案,我将使用此选项。谢谢。
  • 是的,如果你想要更少的代码,你是对的
  • 我已经编辑了我的答案,并建议你什么可以在这里工作,试试吧!!
猜你喜欢
  • 2016-04-08
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 2019-08-17
相关资源
最近更新 更多