2017-12-26 2 views

答えて

0

誰が答えを必要とする場合、これは私の仕事:

this.route.paramMap.subscribe(data => { 
    console.log('this routes to =>', data, name); 
    }); 
-1

:これは、データベース構造である

import { ActivatedRoute } from '@angular/router'; 

export class CategoriesComponent { 

     ngOninit() { 
     this.route$ = this.activatedRoute.queryParams.subscribe((params: 
     Params) => { 
     if (params) { 
     console.log(params['collection']); 
     } else { 
     console.log(params['items']); 
     } 
     }); 

:私はこれがTSである

製品の名前に基づいて、製品ページにルーティングするカテゴリページを必要としますあなたのURLパラメータを購読するngOnInit

ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
    console.log(params['collection']); 
    console.log(params['items']);. 
    }); 
} 
+1

私はどちらも「未定義」になります。 – Famous

+0

あなたのURLはどのように見えますか? –

+0

ここにpublic repo => https://bitbucket.org/FamousTM/angular5com – Famous

0

私は構造とyの命名に関する多くの情報がありませんFirestoreデータベース。

constructor(
    public afAuth: AngularFireAuth, 
    db: AngularFirestore, 
    private route: ActivatedRoute, 
    private productsService: ProductsService, 
    private afs: AngularFirestore 
) { 
    this.afAuth.auth.signInAnonymously(); 
    this.user = this.afAuth.authState; 
    this.route.paramMap.subscribe(params => { 
    this.name = params.get('id') 
    }); 
} 

ngOnInit() { 
    this.itemCollection = this.afs.collection<Item>('items', ref => { 
    return ref.where('name', '==', this.name); 
    }); 
    this.items = this.itemCollection.valueChanges(); 
    this.items.subscribe(data => { 
    console.log(data) 
    }) 
} 

ここでの違いは、私が最初のコンストラクタは、あなたのルートのparamsを購読し、アドレスバーから名前を取得するために許可されていることである。しかし、このショットを与えます。


ソリューション

あなたのルータモジュールで:idという名前のパラメータを持っているので、あなたはこの同じ識別子でパラメータ値を取得する必要があります。

this.route.paramMap.subscribe(params => { 
    this.name = params.get('id') 
}); 

あなたは、このルートのパラメータを取得したクエリ演算子を使用してFirestoreから、一致する文書を要求したら:

this.itemCollection = this.afs.collection<Item>('items', ref => { 
    return ref.where('name', '==', this.name); 
}); 
+0

おっと! 'name.'を' this.name'の代わりに 'ref.where()'メソッドに渡していました。それは今働く。前のコメントを削除してください。 – Trent

関連する問題