【问题标题】:The URL changes back to the previous URL after the page from the current URL is loaded加载当前 URL 的页面后,该 URL 变回之前的 URL
【发布时间】:2017-12-24 06:49:37
【问题描述】:

首先我的网址是:

 /pages/check_info/add_info

点击该页面中的按钮后,它会加载 URL:

/pages/check_info/add_visitor.

问题是在add_visitor 的表单被加载后,URL 又变回了

/pages/check_info/add_info

add_visitor 页面仍然存在。 现在,当我单击一个链接回 add_info 的按钮时,该按钮不会重定向到任何地方,因为当前 URL 是相同的

我用于按钮的代码是

     <div class="btn-area">
      <button md-raised-button class="btn btn-primary btn-rounded"
      type="submit">PLEASE CHECK IN</button>
      <a  routerLink='/pages/check-info/add_info'>BACK</a>
    </div>

我用于路由的代码:

 const routes: Routes = [


   {path: '',redirectTo: 'add_info', pathMatch: 'full'},
  {path: 'add_info', component: AddInfoComponent},
  {path: 'display_info', component: DisplayInfoComponent},
  {path: 'add_visitor', component: AddVisitorComponent},
  {path: 'search_student', component: SearchStudentComponent},
  {path: 'add_student', component: AddStudentComponent},
  {path: 'add_graduate', component: AddGraduateComponent},
  {path: 'check-in', component: AddInfoComponent},

];

【问题讨论】:

  • 你能添加你的代码吗?
  • 使用这个
    BACK 告诉我。
  • 它仍然不起作用,因为它仍然会抛出与页面仍然相同的 url

标签: angular angular-routing angular-router angular4-forms


【解决方案1】:

尝试如下更改您的链接:

<div class="btn-area">
      <button md-raised-button class="btn btn-primary btn-rounded"
      type="submit">PLEASE CHECK IN</button>
      <a  routerLink="['/pages/check-info/add_info']">BACK</a>
    </div>

所以换成

routerLink="['/pages/check-info/add_info']"

希望对你有帮助!

P.S 它总是重定向到 add_info 因为它是默认路由...所以如果它不匹配任何现有路由它总是重定向到它...

如果您想要像 /pages/check-info/add_info 这样的路由,则必须使用子路由:

例如:

 {path: 'pages', component: PageComponent, 
 child[
   { path: 'check-info', component: CheckInfoComponent}
  ]},

等等

【讨论】:

  • 它仍然无法正常工作,因为它仍然尝试重定向到该页面仍在所在的相同 url
【解决方案2】:

问题不在于路径或视图中的代码补丁。如果 TS 中的服务、包或任何内容存在错误,则在 Angular 中,当路径被重定向到包含其中一些错误的页面时,页面会加载,但 url 会更改回之前的 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2018-02-04
    • 2020-09-24
    相关资源
    最近更新 更多