来自外部应用的通话服务

时间:2018-06-28 15:05:08

标签: php angular

我有2个不同的应用程序,一个基于Angular2,另一个使用PHP。 我需要从第二个服务(PHP)中调用第一个服务的功能(Angular)。即使奇怪又丑陋,这有可能吗?

我知道如何在Angular应用程序中调用PHP文件,但不是这样。

这是我的测试文件:

webService.component.ts

import { Component } from '@angular/core';
import {Observable} from "rxjs/Observable";
import {ActivatedRoute, Params} from "@angular/router";
import {WebService} from "./webService.service";

@Component({
    selector: 'webService-element',
    template: `{{result}}`
})

export class WebServiceComponent {
    webService: WebService;
    result: any;

    constructor(private route: ActivatedRoute, webService: WebService) {
        this.webService = webService;
    }

    ngOnInit() {
        this.route.params.subscribe((params: Params) => {
            let aId = params['aId'];
            let bId = params['bId'];
            this.getPif(aId, bId);
        });
    }

    getPif(aId: any, bId: any): any { 
        this.result = this.webService.getPif();
        return this.webService.getPif();
    }
}

webService.service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class WebService {

    constructor(private http: Http) {}

    //getPif(): Observable<any> {
    getPif(): any {
        console.log("rraaahhhhh");
        return "pif";
    }
}

webService.routing.ts

import { Routes, RouterModule } from "@angular/router";
import {WebServiceComponent} from "./webService.component";

const WEBSERVICE_ROUTES: Routes = [
    { path: '', component: WebServiceComponent },
    { path: 'getTest/:aId/:bId', component: WebServiceComponent },
];

export const webServiceRouting = RouterModule.forChild(WEBSERVICE_ROUTES);

webService.module.ts

import { NgModule } from "@angular/core";
import {WebService} from "./webService.service";
import {WebServiceComponent} from "./webService.component";
import {webServiceRouting} from "./webService.routing";


@NgModule({
    imports: [
        webServiceRouting
    ],
    declarations: [
        WebServiceComponent
    ],
    providers: [
        WebService
    ]
})

export class WebServiceModule { }

我尝试启动“ http://localhost:3000/webService/getTest/1/2

非常感谢您的帮助!

0 个答案:

没有答案