查询Angular IO中根组件的参数

时间:2017-11-13 13:36:08

标签: angular angular2-routing angular4-router

我有一个非常简单的Angular 4+应用程序,它从API获取数据并显示数据。我相信只使用根组件就可以实现这一点:E ====================================================================== ERROR: test_start (__main__.TestWeb) ---------------------------------------------------------------------- Traceback (most recent call last): File "...Crawler\crawl_core\src_main\run.py", line 26, in setUp driver = webdriver.PhantomJS(executable_path=phantomjs_path,service_args=service_args) File "...\anaconda3\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 58, in __init__ desired_capabilities=desired_capabilities) File "...\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__ self.start_session(desired_capabilities, browser_profile) File "...\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "...\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute self.error_handler.check_response(response) File "...\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 165, in check_response raise exception_class(value) selenium.common.exceptions.WebDriverException: Message: <HTML><HEAD> <TITLE>Access Denied</TITLE> </HEAD> <BODY> <FONT face="Helvetica"> <big><strong></strong></big><BR> </FONT> <blockquote> <TABLE border=0 cellPadding=1 width="80%"> <TR><TD> <FONT face="Helvetica"> <big>Access Denied (authentication_failed)</big> <BR> <BR> </FONT> </TD></TR> <TR><TD> <FONT face="Helvetica"> Your credentials could not be authenticated: "General authentication failure due to bad user ID or authentication token.". You will not be permitted access until your credentials can be verified. </FONT> </TD></TR> <TR><TD> <FONT face="Helvetica"> This is typically caused by an incorrect username and/or password, but could also be caused by network problems. </FONT> </TD></TR> <TR><TD> <FONT face="Helvetica" SIZE=2> <BR> For assistance, contact your network support team. </FONT> </TD></TR> </TABLE> </blockquote> </FONT> </BODY></HTML> {path: 'main', component: MainComponent}之类的QueryParams。

我能够获取查询参数,但由于某种原因,我的路由器在已经存在的路由+ params部分之后重复路由+ params部分URL。例如,我的本地地址从?id=1变为localhost:4200/main?id=1

ActivatedRoute确实从URL中选择了正确的查询参数,但我希望尽可能保持URL的清理。如何防止这种重复发生?我根据index.html中的路由要求设置了localhost:4200/main?id=1/main?id=1

模块:

<base href='/'>

组件:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule,
    RouterModule.forRoot(
      [
        { path: 'main', component: MainComponent},
      ]),
    CommonModule,
    HttpModule,
    FormsModule,
    NgbModule.forRoot()
  ],
  declarations: [MainComponent],
  providers: [
    {provide: APP_BASE_HREF, useValue: window.document.location.href},
    DataService,
    WindowService,
    HttpUtil
  ],
  bootstrap: [MainComponent]
})
export class MainModule{}

1 个答案:

答案 0 :(得分:0)

这是由APP_BASE_HREF

引起的
{provide: APP_BASE_HREF, useValue: window.document.location.href}

您告诉应用使用window.document.location.href main?id=1作为您的basehref。然后Angular将自己的路由附加到basehref的末尾。这就是你获得重复的原因

 localhost:4200/main?id=1(< APP_BASE_HREF)/main?id=1(< ROUTE)

以下是有关APP_BASE_HREF(https://angular.io/api/common/PathLocationStrategy#description

功能的文档