无法定义类型与类相同的类属性(自引用,如层次结构)

时间:2018-10-25 17:58:44

标签: angular typescript class

我有一个Angular模型类,其中某些属性可以像这样自引用该类:

export class ItemCategory {
  constructor(
    public parentCategory?: ItemCategory, 
    public subCategories?: ItemCategory[], 
    public name?: string, 
    public description?: string
  ) { }
}

Angular编译器向我显示以下错误:

ERROR in src/app/models/entities/itemCategory.model.ts(9,14): error TS2395: Individual declarations in merged declaration 'ItemCategory' must be all exported or all local.
src/app/models/entities/itemCategory.model.ts(28,10): error TS2395: Individual declarations in merged declaration 'ItemCategory' must be all exported or all local.
src/app/models/entities/itemCategory.model.ts(28,10): error TS2440: Import declaration conflicts with local declaration of 'ItemCategory'.

我如何自我参考课程?

编辑:使用此类的文件

src \ app \ models \ services \ itemCategory.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ItemCategory } from "../entities/itemCategory.model";

@Injectable({providedIn: 'root'})
export class ItemCategoryService {
  constructor(private http: HttpClient) { } 

  public getAll() : Observable<ItemCategory[]> {
    return this.http.request<ItemCategory[]>("get", `api/ItemCategory`, { body: null });
  }

  public get(id: number) : Observable<ItemCategory> {
    return this.http.request<ItemCategory>("get", `api/ItemCategory/${id}`, { body: null });
  }

  public add(itemCategory: ItemCategory) : Observable<number> {
    return this.http.request<number>("post", `api/ItemCategory`, { body: itemCategory });
  }

  public update(id: number, itemCategory: ItemCategory) : Observable<number> {
    return this.http.request<number>("put", `api/ItemCategory/${id}`, { body: itemCategory});
  }

  public delete(id: number) : Observable<void> {
    return this.http.request<void>("delete", `api/ItemCategory/${id}`, { body: null });
  }
}

src \ app \ models \ repository.ts

import { Injectable } from '@angular/core';

import { ItemCategory } from "./entities/itemCategory.model";

import { ItemCategoryService } from "./services/itemCategory.service";

@Injectable()
export class Repository {
  constructor(
    private itemCategoryService: ItemCategoryService
  ) {
      this.itemCategoryService.getAll().subscribe(response => this.itemCategories = response);
  }

  itemCategories: ItemCategory[];
}

1 个答案:

答案 0 :(得分:0)

我认为您将在导入时再次声明该类。根据我的理解,我已重现了该问题。

https://stackblitz.com/edit/angular-error-reproduce?file=src/app/hello.component.ts

类似以下内容

<form method="get" id="post" action="<?php echo home_url(); ?>/">
    <input type="search" class="sb-search-input" placeholder="<?php _e('Search Projects...') ?>" name="s" id="s" autocomplete="off" />
    <input type="hidden" value="building" name="post_type" />
    <?php
        // output all of our Categories
        $swp_cat_dropdown_args = array(
                'show_option_all'  => __( 'Select Category' ),
                'include'             => ('423,424,425,426,427,428,429,430,431,432,454,433,434,435,436,437,438,439,440,441,442,443,444,445,446'),
                'orderby'            => 'title',
                'order'              => 'ASC',
                'value_field' => 'slug',
                'name' => 'category'
            );
        wp_dropdown_categories( $swp_cat_dropdown_args );
    ?> <?php
echo "<br>";
?>
    <input type="submit" id="searchsubmit" class="sb-search-submit" value="<?php _e('Search') ?>" />
    <span class="sb-icon-search"></span>
</form>

在以下情况下,您无需再次声明。要使用该类,只需导入并在下面的任意位置使用

//this also causing the issue //you don;t need import here
import { ItemCategory } from './ItemCategory';

export class ItemCategory {
  constructor(
    public parentCategory?: ItemCategory, 
    public subCategories?: ItemCategory[], 
    public name?: string, 
    public description?: string
  ) { }
}