“TypeError.parent.context.car.getBrands不是函数”:s [英] "TypeError.parent.context.car.getBrands is not a function": s

查看:186
本文介绍了“TypeError.parent.context.car.getBrands不是函数”:s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务:

@Injectable()
export class Service {

    constructor(private http: Http) { }

    getCars(){
        return this.http.get...       
    }

    getById(id: string) {
        return this.http.get...       
    }    
}

汽车类:

export class Car {

    private brands: Array<Brand>;

    constructor(public id: string, public name: string) {
        this.brands = new Array<Brand>();
    }

    public getBrands(): Array<Brand> {
        return this.brands;
    }
    //some other methods.
}

然后我有一个Brand类(对象),它有自己的属性和方法,等等...

Then I have a Brand class (object) with it's own attributes and methods and so on and and so on...

car.list.component:

car.list.component:

@Component({
    selector: 'car-list',
    template: `
    //some more code here
    <table>
        <tr *ngFor="let car of cars" >
        <td>{{car.name}}</td>
        <td><button (click)="goToDetail(car)">Detail</button></td>
        </tr>
    </table>
  `,
})


export class ListComponent implements OnActivate {
    id: string
    name: string;
    cars: Array<Car>

    constructor(public _service: Service, public _router: Router) {   }

    routerOnActivate(): void {
        this._service.getCars()
            .subscribe(cars => this.cars = cars);
    }
 }

然后出现问题,car.detail.component :

And then the problem, the car.detail.component:

@Component({
    selector: 'car-detail',
    template: `  
    <h1>Car: {{car?.name}}</h1>
    <hr>
    <div *ngFor="let brand of car?.getBrands(); let i=index">  
        <h2>Brand {{i+1}}</h2>
    </div>   

`,
})

export class DetailComponent implements OnActivate {

    @Input() car: Car;

    constructor(public _service: Service, public _router: Router) {    }

    routerOnActivate(curr: RouteSegment): void {
        let id = curr.getParam('id');
        this._service.getById(id)
            .subscribe(car => {
                this.car = car;
            });
    }
    //some other methods
}

致电时 car.getBrands()它给出错误: TypeError:self.parent.context.car?。getBrands不是函数

推荐答案

我想可能的问题就在这里:

I suppose the likely problem is located here:

 this._service.getById(id)
    .subscribe(car => {
      this.car = car; // you just init object like { id: '1', name: 'name'}
    });

您的服务可能如下所示:

Probably your service looks like this:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json());     
} 

尝试获取汽车来自这样的服务的模型:

Try to get Car model from service like this:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json())
     .map(car => new Car(car.id, car.name));  <== add this line   
}   

这样汽车将是 Car 类的实例,并且将具有适当的方法,例如 getBrands addBrand ...

This way car will be instance of Car class and will have appropriate methods like getBrands, addBrand...

希望有所帮助!

这篇关于“TypeError.parent.context.car.getBrands不是函数”:s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆