angular2路由切换改变页面title的示例代码
- 作者: 没有猫也没有你99386891
- 来源: 51数据库
- 2021-09-02
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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4