Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 元素访问表达式应采用参数_Angular_Typescript - Fatal编程技术网

Angular 元素访问表达式应采用参数

Angular 元素访问表达式应采用参数,angular,typescript,Angular,Typescript,我对Angular是新手,我曾尝试创建一些示例应用程序,但出现了错误。请帮忙。我正试图从谷歌的项目中创造我自己的() 当我从users=User[]中删除数组时它工作正常,但不提供构建。([ts]类型“User[]”不能分配给类型“typeof User”)。使用users=User[]它甚至不编译。(建议[ts]元素访问表达式应采用参数(any)) ////////////////////user.ts/////////////// export class User { id: nu

我对Angular是新手,我曾尝试创建一些示例应用程序,但出现了错误。请帮忙。我正试图从谷歌的项目中创造我自己的() 当我从
users=User[]中删除数组时它工作正常,但不提供构建。(
[ts]类型“User[]”不能分配给类型“typeof User”
)。使用
users=User[]它甚至不编译。(建议[ts]元素访问表达式应采用参数(any))

////////////////////user.ts///////////////

export class User {
    id: number;
    name: string;
  }
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { User } from './user';
// import { USERS } from '../assets/data/userData';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({ providedIn: 'root' })

export class UserService {

  private usersUrl = 'api/users';

  constructor(
    private http: HttpClient) { }

  getUsers(): Observable<User[]> {
    // return of(USERS);
    return this.http.get<User[]>(this.usersUrl);
  }

  /** GET user by id. Will 404 if id not found */
getUser(id: number): Observable<User> {
  const url = `${this.usersUrl}/${id}`;
  return this.http.get<User>(url);
}

/** PUT: update the user on the server */
updateUser (user: User): Observable<any> {
  return this.http.put(this.usersUrl, user, httpOptions);
}

/** POST: add a new user to the server */
addUser (user: User): Observable<User> {
  return this.http.post<User>(this.usersUrl, user, httpOptions);
}

/* GET users whose name contains search term */
searchUsers(term: string): Observable<User[]> {
  if (!term.trim()) {
    // if not search term, return empty hero array.
    return of([]);
  }
  return this.http.get<User[]>(`${this.usersUrl}/?name=${term}`);
}

}
///////////////user.service.ts////////////////

export class User {
    id: number;
    name: string;
  }
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { User } from './user';
// import { USERS } from '../assets/data/userData';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({ providedIn: 'root' })

export class UserService {

  private usersUrl = 'api/users';

  constructor(
    private http: HttpClient) { }

  getUsers(): Observable<User[]> {
    // return of(USERS);
    return this.http.get<User[]>(this.usersUrl);
  }

  /** GET user by id. Will 404 if id not found */
getUser(id: number): Observable<User> {
  const url = `${this.usersUrl}/${id}`;
  return this.http.get<User>(url);
}

/** PUT: update the user on the server */
updateUser (user: User): Observable<any> {
  return this.http.put(this.usersUrl, user, httpOptions);
}

/** POST: add a new user to the server */
addUser (user: User): Observable<User> {
  return this.http.post<User>(this.usersUrl, user, httpOptions);
}

/* GET users whose name contains search term */
searchUsers(term: string): Observable<User[]> {
  if (!term.trim()) {
    // if not search term, return empty hero array.
    return of([]);
  }
  return this.http.get<User[]>(`${this.usersUrl}/?name=${term}`);
}

}
从'@angular/core'导入{Injectable};
从'rxjs'导入{可观察的};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“/User”导入{User};
//从“../assets/data/userData”导入{USERS};
常量httpOptions={
标题:新的HttpHeaders({'Content-Type':'application/json'})
};
@可注射({providedIn:'root'})
导出类用户服务{
private usersUrl='api/users';
建造师(
专用http:HttpClient){}
getUsers():可观察{
//归还(用户);
返回this.http.get(this.usersUrl);
}
/**按id获取用户。如果找不到id,将执行404*/
getUser(id:number):可观察{
常量url=`${this.usersUrl}/${id}`;
返回此.http.get(url);
}
/**PUT:更新服务器上的用户*/
updateUser(用户:用户):可观察{
返回this.http.put(this.usersUrl,user,httpOptions);
}
/**POST:向服务器添加新用户*/
addUser(user:user):可观察{
返回this.http.post(this.usersUrl,user,httpOptions);
}
/*获取名称中包含搜索词的用户*/
searchUsers(术语:字符串):可观察{
如果(!term.trim()){
//如果不是搜索词,则返回空英雄数组。
归还([]);
}
返回this.http.get(`${this.usersUrl}/?name=${term}`);
}
}
更换

users = User[];


这应该可以解决问题。

正如一些人以前写的那样,您尝试将
User[]
添加到只需要一个用户的方法中。通过编写以下代码,可以访问arry的一个元素:

save(): void {
    this.userService.updateUser(this.users[THE_INDEX_FROM_THE_CURRENT_USER]);
}
这应该只给
updateUser()

因此,一般来说:

oneArray: Foo[];         <-   creates an array named 'oneArray' with Foo objects inside
oneFoo = oneArray[0];    <-   Gives you the first element of the array  
oneArray:Foo[] 尝试:


我发现语法的效果更好。

对我来说正确的语法是而不是

user: User[] = User[] // WRONG SYNTAX
但是

users:User[]=新数组()

我在用tsc编译独立的typescript文件时遇到了这个问题。就我而言,我必须改变这一点:

users: User[];
为此:

let users: User[];

不要添加users=User[],因为您正在从服务获取用户。相反,只需声明如下类型 用户:用户[]


上面的操作将起作用

这取决于您试图实现的目标,但这一行
users=User[]是错误的。如果您只想指定类型,它应该是
users:User[]但是如果你想分配空数组,那么
用户:用户[]=[]谢谢。但当我使用users:User[]时,它在save方法中给了我错误。(“User[]”类型的[ts]参数不能分配给“User”类型的参数。“User[]”类型中缺少属性“id”)。您应该包括
userService.updateUser的代码。现在的问题是,这需要一个
用户
,但您正在尝试传递一个
用户[]
。正如Kirk Larkin指出的,您正在将
this.users
(这是一个
用户
的数组)传递给
this.userService.updateUser
(只需要一个
用户
)。你想干什么?您的意思是只更新所选用户还是单独更新每个用户(在这种情况下,您必须循环检查
此。用户
)?我已添加请查看并帮助我。用户:用户[]它在保存方法中给我错误。(“User[]”类型的[ts]参数不可分配给“User”类型的参数。类型“User[]”中缺少属性“id”。)
users: User[] = new Array<User>()
users: User[];
let users: User[];