- 在Spring Boot的Controller中创建返回index.html的Endpoint,如下所示:
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "index.html";
}
}
- 在Angular中创建一个Service,使用HttpClient来获取Spring Boot的Endpoint返回的index.html文件,例如:
@Injectable()
export class HomeService {
constructor(private http: HttpClient) { }
public getHome(): Observable {
return this.http.get('/api/home', {responseType: 'text'});
}
}
- 在Angular的Component中调用HomeService来获取index.html文件,例如:
@Component({
selector: 'app-home',
template: ''
})
export class HomeComponent implements OnInit {
public homeHtml: string;
constructor(private homeService: HomeService) { }
ngOnInit() {
this.homeService.getHome().subscribe(
(response: string) => {
this.homeHtml = response;
},
(error: HttpErrorResponse) => {
console.error(error);
}
);
}
}
- 将Angular的index.html修改为调用Component,如下所示: