Angular 角度帮助:Springboot后端

Angular 角度帮助:Springboot后端,angular,observable,subscribe,Angular,Observable,Subscribe,我正在使用Springboot后端为Angular开发poc。以下代码正在与EmployeeService交互。这部分代码不起作用,控件没有进入警报(“getEmployees2”)。知道这里发生了什么吗 员工列表.component.ts private getEmployees() { //alert("getEmployees " + this.employees); this.employeeService.getEmployeesList().su

我正在使用Springboot后端为Angular开发poc。以下代码正在与EmployeeService交互。这部分代码不起作用,控件没有进入警报(“getEmployees2”)。知道这里发生了什么吗

员工列表.component.ts

private getEmployees() {
    //alert("getEmployees " + this.employees); 
    this.employeeService.getEmployeesList().subscribe(data => {
      alert("getEmployees2" + this.employees.length);
      this.employees = data;
    });
} 
import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';
import { Employee } from './employee';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  
  private baseURL = "http://localhost:8080/api/v1/employees";

  constructor(private httpClient: HttpClient ) { }

  getEmployeesList(): Observable<Employee[]> {
     alert(`${this.baseURL}`);
     return this.httpClient.get<Employee[]>(`${this.baseURL}`);
  }
}
employee.service.ts

private getEmployees() {
    //alert("getEmployees " + this.employees); 
    this.employeeService.getEmployeesList().subscribe(data => {
      alert("getEmployees2" + this.employees.length);
      this.employees = data;
    });
} 
import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';
import { Employee } from './employee';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  
  private baseURL = "http://localhost:8080/api/v1/employees";

  constructor(private httpClient: HttpClient ) { }

  getEmployeesList(): Observable<Employee[]> {
     alert(`${this.baseURL}`);
     return this.httpClient.get<Employee[]>(`${this.baseURL}`);
  }
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“./Employee”导入{Employee};
从“rxjs”导入{Observable};
@注射的({
providedIn:'根'
})
出口级雇员服务{
专用baseURL=”http://localhost:8080/api/v1/employees";
构造函数(私有httpClient:httpClient){}
getEmployeesList():可观察{
警报(`this.baseURL}`);
返回this.httpClient.get(`${this.baseURL}`);
}
}

启用Web安全解决了以下问题:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        // TODO Auto-generated method stub
        httpSecurity.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
        
        
        httpSecurity.csrf().disable();
    }
}

没有机会告诉您,您需要提供更多信息。EmployeeService及其getPloyeesList()方法是什么样子的?您为什么要在设置之前发出此警报?员工?检查浏览器控制台是否有错误。阻止跨源请求:同一源策略不允许读取远程资源。(原因:CORS标头“Access Control Allow Origin”丢失)。首先,您需要从服务获取数据(不管是什么),然后分配它。对于一个简单的修复,您需要交换subscribe方法中的两行。其次,如果由于任何原因无法从服务中获取数据,您也需要修复它。始终使用console.log('your print statement here')而不是alert(''),这样更方便。