加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular6 – Angular 6 – 获取当前路线及其数据

发布时间:2020-12-17 07:10:20 所属栏目:安全 来源:网络整理
导读:你怎么能得到你当前的路线并得到它的数据,孩子和它的父母? 如果这是路线结构: const routes: Routes = [ {path: 'home',component: HomeComponent,data: {title: 'Home'}},{ path: 'about',component: AboutComponent,data: {title: 'About'},children: [
你怎么能得到你当前的路线并得到它的数据,孩子和它的父母?

如果这是路线结构:

const routes: Routes = [
  {path: 'home',component: HomeComponent,data: {title: 'Home'}},{
    path: 'about',component: AboutComponent,data: {title: 'About'},children: [
      {
        path: 'company',component: 'CompanyComponent',data: {title: 'Company'}
      },{
        path: 'mission',component: 'MissionComponent',data: {title: 'Mission'}
      },...
    ]
  },...
]

如果我目前在CompanyComponent,我如何得到我当前的路线w / c是公司,得到它的父母w / c是关于,它的数据和它的兄弟姐妹,如任务等?

解决方法

@Component({...})
export class CompanyComponent implements OnInit {

constructor(
  private router: Router,private route: ActivatedRoute
) {}

ngOnInit() {

  // Parent:  about 
  this.route.parent.url.subscribe(url => console.log(url[0].path));

  // Current Path:  company 
  this.route.url.subscribe(url => console.log(url[0].path));

  // Data:  { title: 'Company' } 
  this.route.data.subscribe(data => console.log(data));

  // Siblings
  console.log(this.router.config);
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读