首页 文章

角度2加载组件太早

提问于
浏览
0

我有一个角度2,它有点按照以下方式构建

app.component - 有几个路由 - 其中一个是some-data-component,它不是首先显示的默认路由 .

some-data-component - 有一个列表,您可以在其中选择项目 - 当它被选中时,我想在数据详细信息组件中显示所选项目 .

事实上,data-detail-component正在应用程序的开头加载 - 我从它的html中得到以下错误

无法读取未定义的属性'学生'

这是有道理的,因为我还没有选择一个项目,所以它应该是未定义的

有没有办法让我构建我的应用程序,这样除非显示它们,否则不会构建其他组件?

这是我的代码:

app.component

import { Component, OnInit } from '@angular/core';
import {Routes, Router, ROUTER_DIRECTIVES} from '@angular/router';

import { Dashboard } from './dashboard/dashboard.component';
import {SomeDataComponent} from './some-data-component/some-data-component';

@Component({
    selector: 'my-app',
    templateUrl: './app/components/app.component.html'
  , directives: [ROUTER_DIRECTIVES] 
})
@Routes([
        { path: '/somedata', component: SomeDataComponent},
        { path: '/dashboard', component: Dashboard }
])
export class AppComponent implements OnInit {

    mobileView: number = 992;
    toggle: boolean = false;

    constructor(private router: Router) {
        this.attachEvents();
    }

    ngOnInit() {
        this.router.navigate(['/dashboard']);
    }
}

一些数据分量

import {SomeDataListView} from '../some-data-list-view/some-data-list-view';
import {DataDetailComponent} from '../data-detail/data-detail.component';

import {SomeService} from '../../services/some_service';    

@Component({
    selector: 'some-data-component',
    providers: [SomeService],
    templateUrl: 'app/components/some-data/some-data.html',
    directives: [SomeDataListView, DataDetailComponent]
})
export class RoutesComponent {
    data;
    selectedData;

    constructor(private someService: SomeService) {
        this.data=[]
    }

    ngOnInit() {
        this.data= this.SomeService.all();
    }

    selectedRouteChanged(route) {
        this.selectedRoute = route;
    }
}

一些-data.html

<rd-widget>
    <rd-widget-body classes="medium no-padding">
        <div>
            <img src="{{route.imgSrc}}" />
            <label>Neighborhood: {{route.name}}</label>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th class="text-center">ID</th>
                            <th>Neighborhood</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr *ngFor="let student of route.students" (click)="setSelected(route)">
                            <td>{{student.id}}</td>
                            <td>{{student.neighborhood}}</td>
                            <td>
                                <span class="{{route.tooltipcls}}">
                                    <i class="fa {{route.icon}}"></i>
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </rd-widget-body>
</rd-widget>

1 回答

  • 1

    您可以使用Elvis运算符:

    <tr *ngFor="let student of route?.students" ...
    

相关问题