のプロパティ「queryParams」を読み取ることができません私は(角度5を使用して)URLからクエリパラメータを取得しようとしている:は未定義
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-shop',
templateUrl: './shop.component.html'
})
export class ShopComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
private sub: any;
private keyword: string;
this.sub = this.route.queryParams.subscribe(params => {
this.keyword = params['keyword'] || null;
});
console.log(this.keyword);
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
私は、コンソールログに次のエラーが表示されます。
shop.component.ts:18 Uncaught TypeError: Cannot read property 'queryParams' of undefined
あなたのコンポーネントは ' router-outlet> 'を持つテンプレートに挿入されていますか、それとも独自のセレクタ「 'ルートを取得するには、 'router-outlet'タグを使うべきです。 –
ConnorsFan