我正在使用Angular9。我创建了一个restapi服务ts,并在其中添加了逻辑代码,因此restapi服务中没有任何问题或错误。在登录组件ts中导入restapi服务时遇到错误。我在应用程序文件夹中创建了一个restapi服务,其中存在我的应用程序路由和应用程序模块。我想将restapi服务导入登录文件夹中的login.component.ts
我在这行出现错误
import { RestapiService } from '../restapi.service';
错误是:
ERROR in src/app/restapi.service.ts:5:1 - error TS1127: Invalid character.
5 � providedIn: 'root' src/app/restapi.service.ts:9:1 - error TS1127: Invalid character.
9 � constructor(private http:HttpClient) { } src/app/restapi.service.ts:12:1 - error TS1127: Invalid character.
12 � const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) }); src/app/restapi.service.ts:13:1
- error TS1127: Invalid character.
13 � return this.http.get("http://localhost:8080/",{headers,responseType: 'text' as 'json'}) src/app/restapi.service.ts:16:1 - error TS1127: Invalid character.
16 � getUsers() { src/app/restapi.service.ts:17:1 - error TS1127: Invalid character.
17 � � let username='Nilmani' src/app/restapi.service.ts:17:3 - error TS1127: Invalid character.
17 � � let username='Nilmani'
src/app/restapi.service.ts:18:1 - error TS1127: Invalid character.
18 � � let password='jglkm@123' src/app/restapi.service.ts:18:3 - error TS1127: Invalid character.
18 � � let password='jglkm@123'
src/app/restapi.service.ts:19:1 - error TS1127: Invalid character.
19 � � const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) }); src/app/restapi.service.ts:19:3
- error TS1127: Invalid character.
19 � � const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
src/app/restapi.service.ts:20:1 - error TS1127: Invalid character.
20 � �return �this.http.get("http://localhost:8080/dashboard",{headers}); src/app/restapi.service.ts:20:3 - error TS1127: Invalid character.
20 � �return �this.http.get("http://localhost:8080/dashboard",{headers});
src/app/restapi.service.ts:20:11 - error TS1127: Invalid character.
20 � �return �this.http.get("http://localhost:8080/dashboard",{headers});
src/app/restapi.service.ts:21:1 - error TS1127: Invalid character.
21 � }
login.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { RestapiService } from '../restapi.service';
@Component({
selector: 'app-dashboard',
templateUrl: 'login.component.html'
})
export class LoginComponent implements OnInit {
username: string;
password: string;
message: any
constructor(private service: RestapiService,private router:Router) { }
ngOnInit() {
}
doLogin() {
let resp = this.service.login(this.username, this.password);
resp.subscribe(data => {
this.message = data;
this.router.navigate(["/home"])
});
}
}
restapi.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class RestapiService {
constructor(private http:HttpClient) { }
login(username:string,password:string){
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
return this.http.get("http://localhost:8080/",{headers,responseType: 'text' as 'json'})
}
getUsers() {
let username='Nilmani'
let password='jglkm@123'
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
return this.http.get("http://localhost:8080/dashboard",{headers});
}
}
答案 0 :(得分:0)
RestApiService-我认为代码是复制粘贴的,其中可能包含一些不可见的字符,导致编译器抛出错误
先尝试将代码粘贴到记事本(或任何不支持标记的编辑器),然后再将其粘贴回编辑器。或者最好尝试从头开始编写代码,看看它是否有效!