【问题标题】:How to use queryParam?如何使用查询参数?
【发布时间】:2019-03-29 11:54:19
【问题描述】:

这里我有一个类项目,我需要创建一个 Booksite,其中有一些书籍信息。现在书籍的信息是从一个 url 获取的。

 myurl  = "http://advjavaapp-env.imadbprai6.us-west- 
2.elasticbeanstalk.com/booksapp/books";

现在我已经获取了详细信息..但是在 Booksite 中有一个导航栏,其中包含诸如 Newarrivals、Select Genre(这是带有选项 Children、Fiction、Mystery 的下拉菜单)和 All Books 等选项。我必须使用 queryParam 来显示根据名称的结果。条件是

对于所有图书,显示的 URL 是 /books。数据是从 myurl/booksapp/books 获取的,其中 myurl 是
http://advjavaapp-env.imadbprai6.us-west-2.elasticbeanstalk.com

Books by Genre 是一个包含 Fiction、Children、Mystery 的下拉列表。显示的 URL 是 /books/selopt,其中 selopt 是下拉列表中的选定选项。数据来自 myurl/booksapp/books/selopt。

对于新品,显示的 URL 是 /books?newarrival=yes。数据来自 myurl/booksapp/books?newarrival=yes

请帮我解决这个问题,因为我从前两天开始尝试这样做。

到目前为止我所做的工作..

在 Booksdetails.Component.ts 中

myurl   = "http://advjavaapp-env.imadbprai6.us-west- 
 2.elasticbeanstalk.com/booksapp/books";

url: URL[] = [];
showmyurl: URL[] = [];
newarrival: string;
url1: string = this.myurl + '?newarrival' + this.newarrival;

ngOnInit() {
  this.route.queryParamMap.subscribe(params => {
  console.log(params);
  this.newarrival = params[ 'newarrival' ];
   });
}

 new(){
   this.netService.getData(this.url1)
   .subscribe(resp => {
    this.showmyurl = resp;
    })
  }

【问题讨论】:

  • 你在showmyurl对象中得到的值是多少?
  • 我什么也没得到..我只是想创建一个函数来获取 url1..但它没有给出任何输出..
  • 在您的myurl 字符串中没有queryParams
  • 你能把你的路由路径贴在这里吗?你能从参数中得到 this.newarrival 吗?
  • { path: 'book', component : BooksdetailsComponent }, { path: 'home', component : HomeComponent }, { path: '', redirectTo : 'home', pathMatch: 'full' } ,

标签: angular service queryparam


【解决方案1】:

把你的代码改成下面的

ngOnInit() {
   this.route.queryParamMap.subscribe(params => {
   console.log(params);
   this.newarrival = params.params[ 'newarrival' ];
  });
}

new(){
 this.url1 = this.myurl + '?newarrival=' + this.newarrival;
 this.netService.getData(this.url1)
 .subscribe(resp => {
  this.showmyurl = resp;
   })
}

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2023-02-11
    • 2016-10-19
    • 2019-07-24
    • 2019-04-29
    • 2016-06-17
    相关资源
    最近更新 更多