我想要实现Angular示例,它从其余的API获取列表。我试过这个:
SQL查询:
@Override
public Iterable<Merchants> findAll() {
String hql = "select e from " + Merchants.class.getName() + " e";
TypedQuery<Merchants> query = entityManager.createQuery(hql, Merchants.class);
List<Merchants> merchants = query.getResultList();
return merchants;
}
休息控制器:
@RestController
@RequestMapping("/merchants")
public class MerchantController {
@GetMapping("/list")
public Iterable<Merchants> getMerchantsList() {
return merchantRepository
.findAll();
}
}
服务:
@Injectable({
providedIn: 'root'
})
export class MerchantService {
constructor(private http: HttpClient) {
}
getList() {
return this.http.get("...../api/merchants/list");
}
}
组件:
@Component({
selector: 'app-terminal',
templateUrl: './terminal.component.html',
styleUrls: ['./terminal.component.scss']
})
export class TerminalComponent implements OnInit {
merchants: Merchant[];
constructor(private merchantService: MerchantService,
private router: Router,
private route: ActivatedRoute) {
}
ngOnInit() {
this.merchantService.getList();
}
}
但是当我通过网页锁定组件时,什么也不会发生。你能给我一些建议吗?我哪里错了?可能我的打字稿哪里不正确?
您需要在Observable
上调用subscribe
,否则它不会使HTTP请求
ngOnInit() {
this.merchantService.getList().subscribe(res => {
console.log("The response is", res);
});
}