如何更改angular2路由器页面标题 [英] how to change page title in angular2 router
问题描述
我想从路由器改变页面的标题,可以这样做?
进口{} 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屋!