您好,登錄后才能下訂單哦!
要在Angular中使用GraphQL客戶端Apollo,您需要按照以下步驟進行配置:
npm install @apollo/client graphql
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular/http';
import { InMemoryCache } from '@apollo/client/core';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
ApolloModule,
HttpLinkModule
],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: (httpLink: HttpLink) => {
return {
cache: new InMemoryCache(),
link: httpLink.create({ uri: 'https://your-graphql-endpoint' })
};
},
deps: [HttpLink]
}
]
})
export class AppModule {}
import { Component } from '@angular/core';
import { Apollo, gql } from 'apollo-angular';
@Component({
selector: 'app-root',
template: `
<div *ngIf="loading">Loading...</div>
<div *ngIf="error">Error: {{ error.message }}</div>
<div *ngIf="data">{{ data }}</div>
`
})
export class AppComponent {
loading = true;
error: any;
data: any;
constructor(private apollo: Apollo) {
this.apollo.query({
query: gql`
query {
hello
}
`
}).subscribe(({ data, loading, error }) => {
this.loading = loading;
this.error = error;
this.data = data;
});
}
}
以上是使用Angular中Apollo客戶端的基本配置和用法。您可以根據您的需求進一步定制和調整配置。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。