我试图调用GET api从仪表板构造函数中获取客户列表,如下所示:
// 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是正确的方法吗?或者我在这里遗漏了什么?
答案 0 :(得分:0)
修改$_SESSION['valOne'] into just: $valTwo
方法,在getCustomers
回调中设置customerList
属性的值。你可以这样做:
subscribe
另外,IIRC,angular文档建议不要在构造函数中进行基本的变量实例化。我让我的组件实现了this.customerService.getCustomers(jwt).subscribe(
(res) => this.customers = res.json(),
(err) => // handle error;
);
接口,并在实现接口时使用OnInit
方法进行API调用。
答案 1 :(得分:0)
道歉。 我在component.ts文件和api控制器中都有相同的方法名称'getCustomers'。
当我键入方法名称以便调用component.ts(dashboard.ts)可视化工作室从api控制器导入的方法,我没有注意到。
当我调用正确的方法时,我的问题就解决了。当然,讨论提供了深思熟虑的解决方案。