gpt4 book ai didi

javascript - AngularJS 版本 6.* 服务方法即将推出未定义

转载 作者:行者123 更新时间:2023-11-28 17:28:49 24 4
gpt4 key购买 nike

所以我只是尝试编写一个组件和服务,从一个 View 获取用户输入并将其传递给 api 进行验证。问题是,在我的组件中,它说该服务本质上没有方法 login 并且未定义。然而,我已经非常仔细地检查并重新检查了 Angular.io 的文档,但无法让任何东西发挥作用。

LoginComponent.ts

import { Component, OnInit } from '@angular/core';
import { UserService } from '../../../services/user.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

constructor(private userService: UserService) {
console.log('userService', userService);
}

ngOnInit() {}

handleSubmit(data) {
// https://api-test.sarahlawrence.edu:82/
this.userService.login(data)
.subscribe(user => console.log('user', user));
}
}

user.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs/index';
import { catchError, map, tap } from 'rxjs/internal/operators';

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

interface CredsInterface {
username: string;
password: string;
};

interface LoggedIn {
token: string;
}

@Injectable({
providedIn: 'root'
})
export class UserService {

private apiUrl = '<apiUrl>';

constructor(
private http: HttpClient
) { }

login (creds: CredsInterface): Observable<any> {
console.log('UserService.login()', creds);

return this.http.post<any>(`${this.apiUrl}/signin`, creds, {})
.pipe(
tap((loggedIn: LoggedIn) => {
console.log(`Login: ${loggedIn}`);
}),
catchError(this.handleError('login()', []))
);
}

/**
* Handle Http operation that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {

// TODO: send the error to remote logging infrastructure
console.error(error); // log to console instead

// TODO: better job of transforming error for user consumption
console.log(`${operation} failed: ${error.message}`);

// Let the app keep running by returning an empty result.
return of(result as T);
};
}
}

我不明白为什么会收到此错误:

Error

所以我注销了服务以查看该对象,奇怪的是该方法被放置在原型(prototype)中:

Prototype

我不明白,我做错了什么吗?

最佳答案

如何调用 handleSubmit 方法?

错误表明它无法读取 undefinedlogin 属性,这意味着 this.userServiceundefined. login 方法位于原型(prototype)内部这一事实是可以的。请记住,获取很深,而集合很浅

我认为您使用某种棘手的方式调用handleSubmit,这使得this引用了您认为的其他对象。

<小时/>

我刚刚看了 stackblitz。您使用 [onHandleSubmit]="handleSubmit" 传递对函数的引用,但当执行时,this 不再是您的 LoginComponent。

将其添加到组件构造函数

this.handleSubmit = this.handleSubmit.bind(this)

有关更多详细信息,请参阅此帖子:Angular pass callback function to child component as @Input

关于javascript - AngularJS 版本 6.* 服务方法即将推出未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50918229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com