すみません。教えてください。
DBから値を取得する処理を外部クラス化にして外だししたら画像のようなエラーとなりました…。の
どのようにすればApolloに対して引数を指定する事ができるのでしょうか?
■ AppComponent.ts
import { Component } from '@angular/core'; import { Name } from './name'; import { MyDB } from './db'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { names: Name[]; db = new MyDB(); // ←ここでエラー ngOnInit() { // 取得 this.names = this.db.Get(); } }
■ db.ts
import { Apollo } from 'apollo-angular'; import { InMemoryCache } from 'apollo-cache-inmemory'; import { HttpLink } from "apollo-angular-link-http"; import gql from 'graphql-tag'; export class MyDB { constructor(private apollo: Apollo, httpLink: HttpLink) { apollo.create({ cache: new InMemoryCache(), link: httpLink.create({uri: "http://localhost:4000/"}) }); } // 取得 public Get(){ this.apollo .watchQuery<any>({query: gql`{ names { id name}} `}) .valueChanges.subscribe(result => { return result.data.names; }); } }
■ AppModule.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { HttpClientModule } from "@angular/common/http"; import { ApolloModule } from "apollo-angular"; import { HttpLinkModule } from "apollo-angular-link-http"; @NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, HttpClientModule, ApolloModule, HttpLinkModule, ], providers: [], bootstrap: [AppComponent] }) export class AppModule {}
回答1件
あなたの回答
tips
プレビュー