[英]Argument of type '{ name: string; id: null; }' is not assignable to parameter of type 'User'
I am making angular app using in memory web api.我正在使用内存 web api 制作角度应用程序。 When I was trying to make
addUser()
function, this is the error I am getting at createUser(data)
:当我尝试创建
addUser()
函数时,这是我在createUser(data)
遇到的错误:
Argument of type '{ name: string;
'{ name: string; 类型的参数id: null;
id:空; }' is not assignable to parameter of type 'User'.
}' 不能分配给'User' 类型的参数。 Types of property 'id' are incompatible.
属性“id”的类型不兼容。 Type 'null' is not assignable to type 'number'.ts(2345)
类型 'null' 不可分配给类型 'number'.ts(2345)
user-list.components.ts:用户列表.components.ts:
import { Component, OnInit } from '@angular/core';
import { User } from 'src/app/users/users.model';
import { UserService } from 'src/app/users/user.service';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css']
})
export class UserListComponent implements OnInit {
user = {
name: '',
id: null
}
edit = true;
add = false;
users!: User[];
constructor(private userService: UserService) { }
ngOnInit(): void {
this.getUsers()
}
private getUsers(){
this.userService.getUsers().subscribe(users => this.users = users)
}
addUser(){
const data = {
name: this.user.name,
id: this.user.id
};
this.userService.createUser(data).subscribe(response => {
console.log(response)
this.getUsers();
})
}
}
user.service.ts:用户服务.ts:
import { Injectable } from '@angular/core';
import { User } from './users.model';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class UserService {
private usersUrl = 'api/users/';
constructor(private http: HttpClient) { }
getUsers(): Observable<User[]> {
return this.http.get<User[]>(this.usersUrl).pipe(
retry(2),
catchError((error: HttpErrorResponse) => {
console.error(error);
return throwError(error);
})
);
}
createUser(user: User): Observable<User> {
user.id === null;
return this.http.post<User>(this.usersUrl, user).pipe(
catchError((error: HttpErrorResponse) => {
console.error(error);
return throwError(error);
})
)
}
editUser(user: User): Observable<any> {
return this.http.put(this.usersUrl + user.id, user);
}
deleteUser(id: number): Observable<any> {
return this.http.delete(this.usersUrl + id);
}
}
This happen due to typescript version 2.1 or later , Please check your tsconfig.json file & search for strictNullChecks , you will see it's value is true.这是由于打字稿版本 2.1 或更高版本而发生的,请检查您的 tsconfig.json 文件并搜索strictNullChecks ,您将看到它的值为 true。
I Believe that in your User Model, the parameter id has type number , but inside your component you created a default user object, with default values, inside that object the id parameter has null value,我相信在您的用户模型中,参数 id 的类型为number ,但是在您的组件中,您创建了一个默认用户对象,具有默认值,在该对象中, id 参数具有空值,
Due to this null value, it's throw such error, I will suggest you that please update your default user object of component with type User Model, after updating, your code will be look like below由于这个空值,它会抛出这样的错误,我建议您更新类型为 User Model 的组件的默认用户对象,更新后,您的代码将如下所示
user: User = {
name: '',
id: 0
}
Now If you assign null to id paramter , the IDE or Code Editor itself show you error, similar to this现在,如果您将 null 分配给id 参数,IDE 或代码编辑器本身会向您显示错误,类似于此
Type 'null' is not assignable to type 'number'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.