要解决"AG_Grid,Angular-Apollo,Apollo查询的数据无法生成网格的行数据"的问题,可以按照以下步骤进行:
确保你的Apollo查询能够成功地从后端获取到数据。你可以使用Apollo DevTools来验证查询是否返回了正确的数据。
确保你已经正确地配置了AG_Grid和Angular-Apollo。确保你已经在Angular模块中导入了AG_Grid模块和Apollo模块,并正确地配置了Apollo客户端。
确保你的AG_Grid配置正确,并且绑定了正确的数据源。在AG_Grid的配置中,你需要指定columnDefs(列定义)和rowData(行数据)属性。确保rowData属性已经正确地绑定到Apollo查询返回的数据。
下面是一个示例的解决方法:
npm install ag-grid-community ag-grid-angular
npm install apollo-angular apollo-angular-link-http apollo-client apollo-cache-inmemory graphql
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AgGridModule } from 'ag-grid-angular';
import { HttpClientModule } from '@angular/common/http';
import { ApolloModule, Apollo } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
AgGridModule.withComponents([]),
ApolloModule,
HttpLinkModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(apollo: Apollo, httpLink: HttpLink) {
apollo.create({
link: httpLink.create({ uri: 'http://localhost:3000/graphql' }),
cache: new InMemoryCache()
});
}
}
import { Component, OnInit } from '@angular/core';
import { Apollo } from 'apollo-angular';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import gql from 'graphql-tag';
@Component({
selector: 'app-root',
template: `
`,
styles: [`
.ag-theme-balham {
display: block;
height: 100%;
}
`]
})
export class AppComponent implements OnInit {
columnDefs = [
{ headerName: 'ID', field: 'id' },
{ headerName: 'Name', field: 'name' },
{ headerName: 'Age', field: 'age' }
];
rowData$: Observable;
constructor(private apollo: Apollo) {}
ngOnInit() {
this.rowData$ = this.apollo
.watchQuery({ query: gql`{ users { id, name, age } }` })
.valueChanges
.pipe(map(result => result.data.users));
}
}
在上面的示例中,我们首先在AppModule中配置了Apollo和AG_Grid模块。然后,在AppComponent中,我们定义了一个AG_Grid,并使用Apollo查询的返回数据绑定了rowData属性。
这样,当应用程序启动时,AG_Grid将根据Apollo查询返回的数据动态生成行数据。
请注意,上述示例假设你已经正确配置了GraphQL服务器,并且可以从"http://localhost:3000/graphql"端点获取到数据。如果你的后端配置不同,请相应地更改上述示例中的URI。
希望这个示例可以帮助你解决AG_Grid,Angular-Apollo和Apollo查询数据无法生成网格行数据的问题。