我正在尝试从url获取查询参数(使用Angular 5):
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未捕获的TypeError:无法读取属性 'queryParams'未定义
答案 0 :(得分:0)
尝试删除this.sub =
访问路由。您也可以尝试通过dot:
this.route.queryParams.subscribe(params => {
this.keyword = params.keyword || null;
});
答案 1 :(得分:0)
事实证明,我已将ngOnInit()
变量设置在导致错误的@RequestMapping(method=RequestMethod.POST, value="/provinces")
public void addProvince(@RequestBody Province province) {
provinceService.addProvince(province);
}
内。呵呵...!