にエラーがスロー私は以下のようなダッシュボードのコンストラクタから顧客のリストを取得するには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を呼び出すのは正しい方法ですか?それとも、私はここに何もないのですか?
コンストラクタでそれを呼び出すことは、その種類に依存します。コンストラクタでできるだけ少ない作業をしたいと思っています。あなたはクラスをインスタンス化するのに十分なだけの処理をしたいと思っています。したがって、顧客がクラスをインスタンス化する必要がない場合は、 'ngOnInit'メソッドで呼び出しを行うことをお勧めします。 – ToddB
どのバージョンのTypeScriptですか? GitHubで[この問題](https://github.com/Microsoft/TypeScript/issues/6046)を見ましたか? – ToddB
あなたのフィードバックのためにToddに感謝します。私はngOnInit()でapi呼び出しを移動しました。 私はtypescriptの2.5.3バージョンを使用しています。 私の角サーバに必要でない/ apiフォルダを除外しようとしましたが、上記のプロジェクト構造のスナップショットを参照してください。 – notsogoodcoder