当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angular2路由切换改变页面title的示例代码

angular2路由切换改变页面title的示例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

甘理梁,李小冉钟汉良,2016高考分数线预测

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);

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网