如何从IONIC 4中的服务中获得价值?

时间:2019-09-23 15:18:35

标签: laravel ionic-framework

我正在开发IONIC中的应用程序,该应用程序通过Laravel中的API从数据库接收值,我在服务中获取了值,但无法将此值传递给我正在调用服务的另一个页面

我的服务功能如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>

        <!-- Camel -->

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-google-pubsub-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-zipfile</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http</artifactId>
            <version>2.23.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>2.23.1</version>
        </dependency>

        <!-- GCP -->
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-storage</artifactId>
            <version>1.86.0</version>
        </dependency>


        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-firestore</artifactId>
            <version>1.17.0</version>
        </dependency>


        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我称之为Service的函数是这样:

get(){
const headers = new HttpHeaders({
      'Authorization': this.token["token_type"]+" "+this.token["access_token"]
    });
return new Promise ((resolve)=>{
     this.http.get(this.env.API_URL+'auth/get',{ headers: headers }).subscribe((result:any)=>{
      this.date = result; //thats ok
      console.log(this.date);
      return this.date;
    },
    (error)=>{
      console.log(error);
    });
   });
}

但是它甚至没有进来,它直接掉了出来。我弄错了吗?

1 个答案:

答案 0 :(得分:0)

尝试这种方式。

getReuniao(): Observable<any> {

return this.http.get<any>( this.env.API_URL+'auth/getreuniao')
.pipe(
retry(1),
catchError(this.handleError)
)           

}; 在同一服务中处理错误。ts:

handleError(error) {
let errorMessage = '';
if(error.error instanceof ErrorEvent) {
// Get client-side error
errorMessage = error.error.message;
} else {
// Get server-side error
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
window.alert(errorMessage);
return throwError(errorMessage);
}

现在,我们将其称为api服务。不要忘记将服务添加到提供者并在构造上调用它。

this.api.getReuniao().subscribe(data=>{ console.log("data:" + data)}, error=>{ console.log("error: " + error});

使用可观察的订阅代替。