Angular 5客户端路由正在干扰我的烧瓶端点

时间:2018-02-15 15:24:00

标签: flask routing angular5

我设置了一个烧瓶服务器,并且我已使用ng build生成我放在静态文件夹中的静态文件。现在,flask为index.html服务没有问题。但我也在同一个烧瓶服务器上设置了API端点。我注意到当我尝试访问API端点时;角度劫持' GET'请求。因此,我(也不是应用程序)可以访问API端点。

我有什么方法可以将请求从角度交给烧瓶吗?

1 个答案:

答案 0 :(得分:0)

您是否创建了一个角色服务来处理API端点?请参考下面的例子

<强> some.service.ts

import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';

const httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable()
export class SomeService {

    constructor(private http:HttpClient) {}

    getAPIMethod() {
        return this.http.get('/api/some_method_endpoint');
    }
}

<强> app.component.ts

import {Component} from '@angular/core';
import {SomeService} from './some.service';
import {Observable} from 'rxjs/Rx';

export class AppComponent {

    ngOnInit() {
       this.get_data_from_service();
    }

    constructor(private _someService: SomeService) { }

    get_data_from_service() {
        this._someService.getAPIMethod().subscribe(
          data => { 
            // Your handler code here?
          },
          err => console.error(err),
          () => console.log('Completed the call')
        );
      }
    }
}
相关问题