2017-12-26 18 views
0

にエラーがスロー私は以下のようなダッシュボードのコンストラクタから顧客のリストを取得するにはGET APIを呼び出すためにしようとしている:はtypescriptですコンストラクタでAPIを呼び出すと、エラーTS5055

 // tslint:disable-next-line:max-line-length 
    constructor(public addCustomerDialog: MatDialog, private router: Router, private route: ActivatedRoute, private customerService: CustomerService) { 
    this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/login'; 
    this.customerList = getCustomers(); 
    } 

    getCustomers() { 
    const jwt = localStorage.getItem('jwt'); 

     // call AJAX 
    this.customerService.getCustomers(jwt).subscribe((res) => { // <--- 
     console.log(res); 
    }, (err) => { // <--- 
     alert('Token expired. Need to login again!'); 
     this.router.navigateByUrl(this.returnUrl); 
     console.log(err); 
    }); 

    } 

私はエラーの下に取得しています:

ERROR in error TS5055: Cannot write file '/home/spartan/Documents/development/ktf-standalone/api/config/main.js' because it would overwrite input file. 
    Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. 
error TS5055: Cannot write file '/home/spartan/Documents/development/ktf-standalone/api/controllers/controllerCustomer.js' because it would overwrite input file. 
    Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. 
error TS5055: Cannot write file '/home/spartan/Documents/development/ktf-standalone/api/models/customer.js' because it would overwrite input file. 
    Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. 

私のルートフォルダにtsconfig.jsonファイルがあります。

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "outDir": "./dist/out-tsc", 
    "sourceMap": true, 
    "declaration": false, 
    "moduleResolution": "node", 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "target": "es5", 
    "typeRoots": [ 
     "node_modules/@types" 
    ], 
    "lib": [ 
     "es2017", 
     "dom" 
    ], 
    "allowJs": true 
    } 
} 

コンストラクタでいくつかの行をコメントアウトすると、私のサーバを起動できます。

コンストラクタでAPIを呼び出すのは正しい方法ですか?それとも、私はここに何もないのですか?

プロジェクト構造: enter image description here

+1

コンストラクタでそれを呼び出すことは、その種類に依存します。コンストラクタでできるだけ少ない作業をしたいと思っています。あなたはクラスをインスタンス化するのに十分なだけの処理をしたいと思っています。したがって、顧客がクラスをインスタンス化する必要がない場合は、 'ngOnInit'メソッドで呼び出しを行うことをお勧めします。 – ToddB

+1

どのバージョンのTypeScriptですか? GitHubで[この問題](https://github.com/Microsoft/TypeScript/issues/6046)を見ましたか? – ToddB

+0

あなたのフィードバックのためにToddに感謝します。私はngOnInit()でapi呼び出しを移動しました。 私はtypescriptの2.5.3バージョンを使用しています。 私の角サーバに必要でない/ apiフォルダを除外しようとしましたが、上記のプロジェクト構造のスナップショットを参照してください。 – notsogoodcoder

答えて

0

subscribeコールバックでcustomerListプロパティの値を設定するために、あなたのgetCustomers方法を変更します。あなたが好きなものでそれを行うことができます。また

this.customerService.getCustomers(jwt).subscribe(
    (res) => this.customers = res.json(), 
    (err) => // handle error; 
); 

、IIRC、角度のドキュメントは、コンストラクタでの基本的な変数のインスタンスの横にはあまりやってないことをお勧めします。私は自分のコンポーネントにOnInitインターフェイスを実装させ、インターフェイスを実装するときにはngOnInitメソッドで必要なAPI呼び出しを行います。

+0

ご意見ありがとうございます。あなたの提案に応じて変更した後でも、私は同じエラーが発生しています。 – notsogoodcoder

0

謝罪します。 私はcomponent.tsファイルとapiコントローラの両方で同じメソッド名 'getCustomers'を持っていました。

component.ts(dashboard.ts)ビジュアルスタジオのインポートメソッドを呼び出すためにメソッド名を入力したときに、私が気付かなかったapi controllerからメソッドをインポートしました。

私が正しい方法を呼んだとき、私の問題は解決されました。そして確かに議論は解決を導く思考のための食糧を与えました。

関連する問題