Http &引用;TypeError.parent.context.car.getBrands不是函数";:s

Http &引用;TypeError.parent.context.car.getBrands不是函数";:s,http,typescript,angular,Http,Typescript,Angular,服务: @Injectable() export class Service { constructor(private http: Http) { } getCars(){ return this.http.get... } getById(id: string) { return this.http.get... } } 车辆等级: export class Car {

服务:

@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.
}
出口级轿车{
自有品牌:阵列;
构造函数(公共id:string,公共名称:string){
this.brands=新数组();
}
public getBrands():数组{
退回这个品牌;
}
//其他一些方法。
}
然后我有一个品牌类(对象),它有自己的属性和方法等等

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);
    }
 }
@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.name}
细节
`,
})
导出类ListComponent实现OnActivate{
id:字符串
名称:字符串;
汽车:阵列
构造函数(公共_服务:服务,公共_路由器:路由器){}
routerOnActivate():void{
这个._service.getCars()
.订阅(cars=>this.cars=cars);
}
}
然后是问题,car.detail.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);
    }
 }
@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?.name}

品牌{{i+1}} `, }) 导出类DetailComponent实现OnActivate{ @输入()汽车:汽车; 构造函数(公共_服务:服务,公共_路由器:路由器){} 路由激活(当前:路由分段):无效{ 设id=curr.getParam('id'); 此._服务.getById(id) .订阅(汽车=>{ 这辆车; }); } //其他一些方法 }

调用
car.getBrands()
时会出现错误:
TypeError:self.parent.context.car?.getBrands不是一个函数

我想可能的问题在这里:

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

您的服务可能如下所示:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json());     
} 
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   
}   
尝试从以下服务获取
汽车
型号:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json());     
} 
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   
}   
getById(id:string){
返回此.http.get('app/car.json')
.map(data=>data.json())

.map(car=>newcar(car.id,car.name));我想可能的问题在这里:

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

您的服务可能如下所示:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json());     
} 
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   
}   
尝试从以下服务获取
汽车
型号:

getById(id: string) {
   return this.http.get('app/car.json')
     .map(data => data.json());     
} 
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   
}   
getById(id:string){
返回此.http.get('app/car.json')
.map(data=>data.json())

.map(car=>newcar(car.id,car.name));您在何处使用
self.parent.context.car.getBrands
?您问题中的代码没有显示任何内容。您使用的浏览器是什么?是否可以尝试强制重新加载(Chrome中的Ctrl+F5或Ctrl+r)无法在chrome或firefox中强制重新加载。我想我已经提供了所需的所有代码??处理汽车http(检索、添加等)的服务然后是包含品牌对象数组的汽车对象。详细信息组件是问题所在,当我尝试检索所选特定汽车的品牌时,我会使用该组件。调用位于汽车详细信息模板中。我假设错误消息中提到的代码是问题所在;-),而这不包括在您的问题中。浏览器不会引入va变量如
self
,因此它必须来自您的代码。这是第一部分中存在的所有代码,因此在将db连接到我的代码后,我甚至没有进一步了解。我在细节组件的模板中还有一些代码,但实际上我昨天已经删除了它(以防万一),所以我确切地知道问题出在哪里,之后没有代码会把它搞糟。所以我的代码一直工作到我调用car.getBrands的时候。所以这实际上是我在我的应用程序中使用的所有代码。所以我不知道…?我猜你的应用程序中有这样的代码,因为它看起来不像来自其他地方的代码。在这种情况下,这是您的系统的问题。请尝试清除浏览器中的缓存并杀死您正在使用的HTTP服务器,然后重试。您在哪里使用
self.parent.context.car.getBrands
?问题中的代码没有显示任何内容。您使用的浏览器是什么?您可以尝试强制重新加载(Chrome中的Ctrl+F5或Ctrl+r)无法在chrome或firefox中强制重新加载。我想我已经提供了所需的所有代码??处理汽车http(检索、添加等)的服务然后是包含品牌对象数组的汽车对象。详细信息组件是问题所在,当我尝试检索所选特定汽车的品牌时,我会使用该组件。调用位于汽车详细信息模板中。我假设错误消息中提到的代码是问题所在;-),而这不包括在您的问题中。浏览器不会引入va变量如
self
,因此它必须来自您的代码。这是第一部分中存在的所有代码,因此在将db连接到我的代码后,我甚至没有进一步了解。我在细节组件的模板中还有一些代码,但实际上我昨天已经删除了它(以防万一),所以我确切地知道问题出在哪里,之后没有代码会把它搞糟。所以我的代码一直工作到我调用car.getBrands的时候。所以这实际上是我在我的应用程序中使用的所有代码。所以我不知道…?我猜你的应用程序中有这样的代码,因为它看起来不像来自其他地方的代码。在这种情况下,这是您的系统的问题。请尝试清除浏览器中的缓存并终止正在使用的HTTP服务器,然后重试。