如何更改angular2路由器页面标题 [英] how to change page title in angular2 router

查看:1107
本文介绍了如何更改angular2路由器页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从路由器改变页面的标题,可以这样做?

 进口{} RouteConfig从'angular2 /路由器;
@RouteConfig([
  {路径:'/家',组成:HomeCmp,名称:HomeCmp'}
])
一流的MyApp {}


解决方案

标题服务@EricMartinez指出有一个的setTitle()法 - 这一切您需要设置标题。

在自动做路线的变化方面,的截至目前的还有比订阅的 路由器 并调用的setTitle()在回调

 进口{} RouteConfig从'angular2 /路由器;
从angular2 /平台/浏览器的进口{标题};@RouteConfig([
  {路径:'/家',组成:HomeCmp,名称:HomeCmp'}
])
类的MyApp {
    构造器(路由器:路由器,标题:标题){
       router.subscribe((URL)=>在每一个URL变化{//火灾
          title.setTitle(getTitleFor(URL));
       });
    }
 }

不过,我要强调的截至目前的,因为路由器仍处于开发状态,我希望(或者至少是希望),我们将通过<$ C $能够做到这一点C> RouteConfig 。

I am trying to change the page title from the router, can this be done?

import {RouteConfig} from 'angular2/router';
@RouteConfig([
  {path: '/home', component: HomeCmp, name: 'HomeCmp' }
])
class MyApp {}

解决方案

The Title service @EricMartinez points out has a setTitle() method - that's all you need to set the title.

In terms of doing it automatically on route changes, as of now there's no built-in way of doing this other than subscribing to Router and calling setTitle() in your callback:

import {RouteConfig} from 'angular2/router';
import {Title} from 'angular2/platform/browser';

@RouteConfig([
  {path: '/home', component: HomeCmp, name: 'HomeCmp' }
])
class MyApp {
    constructor(router:Router, title:Title) {
       router.subscribe((url)=>{ //fires on every URL change
          title.setTitle(getTitleFor(url));
       });
    }
 }

That said, I emphasize as of now because the router is still under heavy development, and I expect (or at least hope) that we'll be able to do this via RouteConfig in the final release.

这篇关于如何更改angular2路由器页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆