Angular中的getCurrentNavigation().extras指的是从当前导航中获取到的额外信息。
示例代码:
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router';
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponentComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
const extras = this.router.getCurrentNavigation().extras;
console.log('额外信息:', extras);
}
}
在这个示例代码中,我们通过调用router对象的getCurrentNavigation()方法来获取到当前导航的额外信息,然后将其打印到控制台上。
需要注意的是,extras属性并不是所有的导航都会有,只有在通过路由参数导航时才会有额外信息。如果没有额外信息,则该属性值为null。