angular2路由切换改变页面title的示例代码
更新时间:2017年08月23日 16:41:43 作者:avery1
本篇文章主要介绍了angular2路由切换改变页面title的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
angular2里默认切换路由或者切换组件,页面的title是不会变化的。
angular2在路由设置里提供了data参数可以传值,如下
{ path: 'home', component: HomeComponent, data: { title: 'Home', aaa: 'aaaa', bbb: 'bbbb', ccc: "cccc"} }
path和component是常用的属性,path是地址栏的显示,component是调用的组件。
data则可以传数据,在组件内可以调用。
参数调用
angular2提供Title服务可以修改title。
路由内获取设置的参数可以用ActivatedRoute的snapshot的data属性获取
如下:
import { ActivatedRoute } from '@angular/router'; import { Title } from '@angular/platform-browser'; config: any; constructor( private route: ActivatedRoute, private titleService: Title ) { } ngOnInit(): void { // Get the config information from the app routing data this.config = this.route.snapshot.data; // Sets the page title this.titleService.setTitle(this.config.title); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Angular2中如何使用ngx-translate进行国际化
本篇文章主要介绍了Angular2中使用ngx-translate进行国际化,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05详解Webstorm 下的Angular2.0开发之路(图文)
这篇文章主要介绍了详解Webstorm 下的Angular2.0开发之路(图文) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-12-12
最新评论