[英]Angular Reload page ngSubmit
当我第一次提交表格时
但是当我再次提交表单时,它会更改 url 但不会更新页面
app.component.ts
searchString: String;
constructor(private router: Router){}
handleSearch(){
this.router.navigateByUrl(`/search?q=${this.searchString}`);
}
app.component.html
<form (ngSubmit)='handleSearch()'><input matInput type="text" name="searchString" [(ngModel)]='searchString'></form>
应用程序路由.module.ts
{path: "search", component: SearchResultComponent},
搜索结果.component.ts
export class SearchResultComponent implements OnInit {
results:any;
searchQuery:any;
private routeSub: Subscription;
constructor(private music: MusicDataService,private route: ActivatedRoute) { }
ngOnInit(): void {
this.routeSub = this.route.queryParams.subscribe(params =>{
this.searchQuery = params['q'] || 0;
})
this.music.searchArtists(this.searchQuery).subscribe(
data =>{
this.results = data.artists.items.filter(item => item.images.length > 0);
console.log(this.results);
}
)
}
}
ngOnInit(): void {
this.router.routeReuseStrategy.shouldReuseRoute = () => {
return false;
};
}
似乎在 app.component.ts 中解决了我的问题想知道它是如何工作的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.