我刚开始学习角,现在我正在开发一个小的应用程序来实践它,后端侧我春天启动开发它。
我做了一个服务学生从数据库MySQL的让所有的学生中,API剩下的就是工作。
这是我在控制台中得到了错误:analytics.js的:1个GET http://makeappdev.xyz/1e6ab715a3a95d4603.js网:: ERR_NAME_NOT_RESOLVED
import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { StudentService } from '../services/student.service';
@Component({
selector: 'app-list-student',
templateUrl: './list-student.component.html',
styleUrls: ['./list-student.component.css']
})
@Injectable()
export class ListStudentComponent implements OnInit {
listStudent;
currentStudent;
constructor(private httpClient: HttpClient, private studentService : StudentService) { }
ngOnInit() {
this.studentService.getListStudent();
this.listStudent = this.studentService.listStudent;
}
--
当我直接从组件调用GET方法,它的工作原理反对,当我试图把它放在它不工作了服务
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class StudentService {
public listStudent;
constructor(private httpClient: HttpClient) { }
public getAllStudent() {
this.httpClient
.get('http://localhost:8080/etudiants')
.subscribe(
(response) => {
this.listStudent = response;
console.log('Ok ! : ');
},
(error) => {
console.log('Ko ! : ' + error);
}
);
}
public getListStudent() {
this.getAllStudent();
return this.listStudent;
}
}
先感谢您
你有什么应该工作,除非你有你的终点可能的连接。这可能意味着无论你的域名访问需要更新自己的域名服务器。
ERR_NAME_NOT_RESOLVED意味着该域名无法解析。 DNS(域名系统)是负责解析域名,并在互联网上的每个域都有一个名称服务器,使人们有可能对DNS的解析域名。
您使用的浏览器?如果是的话,你可以尝试清除主机缓存,或者尝试不同的浏览器。
除此之外我可能会重构这个有点。你调用一个异步请求,但在你的服务传递到公共存储的值参考组件。要清理干净,你可以通过传递一个Observable
回到你的组件来访问的响应,以及,存储列表在服务访问之后。我可能会在服务上的私人名单,但并提供一个getter所以它不能直接更新。
你可以试试这个Stackblitz您的端点,看看,因为它的工作原理采用reqres
如果仍不能成功,这其中包括我上面建议。
https://stackblitz.com/edit/angular-mcu7my?embed=1&file=src/app/app.component.html