angular6 监听url变化
发布时间:2020-12-17 17:30:33 所属栏目:安全 来源:网络整理
导读:angular组件的类中的ngOnInit方法只会在页面首次加载时执行,当我们在页面中需要根据不同的url查询参数显示不同的内容时,就需要监听url的变化。 import { Component,OnInit } from ‘@angular/core‘ ;import { Router,NavigationEnd,ActivationEnd } from
angular组件的类中的ngOnInit方法只会在页面首次加载时执行,当我们在页面中需要根据不同的url查询参数显示不同的内容时,就需要监听url的变化。 import { Component,OnInit } from ‘@angular/core‘; import { Router,NavigationEnd,ActivationEnd } from ‘@angular/router‘; @Component({ selector: ‘app-root‘,templateUrl: ‘./app.component.html‘,styleUrls: [‘./app.component.css‘] }) export class AppComponent implements OnInit { constructor(private router: Router) { this.router.events.subscribe((event: NavigationEnd) => { if (event instanceof ActivationEnd) { sort = event.snapshot.queryParams.sort; // sort为url的查询参数 } }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |