如何在angular2中的一个组件中拥有多个视图

问题描述 投票:2回答:1

我有一个代表导航栏full-layout.component.ts的组件。导航栏中的项目是从服务器接收的(但出于此问题的目的,我将进行硬编码)。

对于每个项目,都有一个与之关联的routerLink,因此当用户点击任何项目时,它会被定向到名为department.component.ts的组件。在这个组件中,提供了一个DepartmentService,它使http get()请求并返回一个字符串数组。

目前,当我在full-layout.component.ts并点击任何项目时,它会完美地指示我并显示正确的数据。但是当我尝试从那里点击另一个项目时,它没有做任何事情,我不知道为什么。

注意:导航栏中的所有项目都显示服务器中的部门列表。

这是我的代码:

ul由我的物品组成

full-layout.component.html

<ul class="nav-dropdown-items">
  <li class="nav-item" *ngFor="let item of dropDownItems">
    <a #clicked class="nav-link" routerLinkActive="active" [routerLink]="[item.routerLink]" (click)="send(item.name)" >
      <i class="icon-puzzle"></i>{{item.name}}
    </a>
  </li>
</ul>

full-layout.component.ts

@Component({
  selector: 'app-dashboard',
  templateUrl: './full-layout.component.html',
  providers: [SharedService]
})
export class FullLayoutComponent implements OnInit {
  service;

  constructor(service: SharedService) { this.service = service; }
  ngOnInit() {}

  send(str) { this.service.saveData(str); }

  dropDownItems = [{
      routerLink: '/components/departments',
      name: 'Artshums'
    },
    {
      routerLink: '/components/departments',
      name: 'Dentistry'
    },
    {
      routerLink: '/components/departments',
      name: 'Law'
    },
    {
      routerLink: '/components/departments',
      name: 'IOPPN'
    },
    {
      routerLink: '/components/departments',
      name: 'LSM'
    },
    {
      routerLink: '/components/departments',
      name: 'NMS'
    },
    {
      routerLink: '/components/departments',
      name: 'Nursing'
    },
    {
      routerLink: '/components/departments',
      name: 'SSPP'
    },
    {
      routerLink: '/components/departments',
      name: 'Health'
    }
  ];
}

departments.component.ts

@Component({
  selector: 'app-departments',
  templateUrl: './departments.component.html',
  providers: [DepartmentsService]
})
export class DepartmentsComponent implements OnInit {
  router; service; myName; _faculty: Faculty;

  constructor(service: SharedService, private departmentsService: DepartmentsService) {
    this.service = service;
    this.myName = service.getData();
  }

  ngOnInit() {
    console.log(this.myName);
    this.departmentsService.getData(this.myName).then(
      (faculties: Faculty) => this._faculty = faculties
    );
  }
}

departments.service.ts

@Injectable()
export class DepartmentsService {
  constructor(
    private http: Http
  ) {}

  getData(facultyName): Promise < any > {
    return this.http.get('/admin/faculty/' + facultyName).toPromise().then(response => response.json()).catch(DepartmentsService.handleError);
  }
}

departments.component.html

<ul class="departments-box" *ngFor="let fac of _faculty">
  <li *ngFor="let dep of fac.Departments">
    {{dep}}
  </li>
</ul>

Question

当用户处于任何项目时,如何实现该功能,它应该显示正确的信息和更新视图?

angular typescript angular2-routing angular2-services angular2-components
1个回答
0
投票

您可以在angular2中使用route参数和ActivatedRoute

通过将名称作为路由参数传递,在您的full-layout.html中进行修改。

 <a #clicked class="nav-link" routerLinkActive="active" [routerLink]="
           [item.routerLink,item.name]"> 

访问应用程序部门中的参数

import { ActivatedRoute } from '@angular/router'

departmentName: string = ""; // used for the route parameter.

constructor(private route: ActivatedRoute,.....) {
    this.departmentName= route.snapshot.params.name;

  }

ngOnInit(){
   this.departmentsService.getData(this.myName).then(
      (faculties: Faculty) => this._faculty = faculties
    );
}

您的路由网址应修改为

/components/departments/:name
© www.soinside.com 2019 - 2024. All rights reserved.