【问题标题】:Routing always reloads the page路由总是重新加载页面
【发布时间】:2018-03-13 00:46:15
【问题描述】:

当我使用从 1 个组件到下一个组件的路线导航时,angular 总是重新加载应用程序。事件顺序:

  • 加载主页 (locahost:4200)
  • 重定向到位置 (localhost:4200/locations)
  • 点击按钮路由到(localhost:4200/items/1)
  • 显示项目页面
  • 然后它会自动路由到 (localhost:4200/#) [如何阻止它这样做?]
  • 然后它路由回位置 (localhost:4200/locations)

思考出了什么问题以及为什么它没有在项目页面上停止?我一直在努力找出一些我确定是简单的用户错误的东西(只剩下什么)。

此外,chrome 控制台或 webstorm 终端中没有错误。我正在使用角度 4.2.4。

这是我的组件:

应用模块

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';

import {AppComponent} from './app.component';
import {LocationComponent } from './location/location.component';
import {ItemsComponent } from './items/items.component';

import {AppRoutingModule} from './app-routing.module';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule
  ],
  declarations: [
    AppComponent,
    LocationComponent,
    ItemsComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

应用路由

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { ItemsComponent } from './items/items.component';
import {LocationComponent} from './location/location.component';

const routes: Routes = [
  { path: 'locations',  component: LocationComponent },
  { path: 'items/:id',  component: ItemsComponent },
  { path: '', redirectTo: '/locations', pathMatch: 'full' }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes,  { enableTracing: true }) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

应用组件

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div>Hello</div><router-outlet></router-outlet>`
})
export class AppComponent { }

位置组件

import {Component, OnInit} from '@angular/core';
import {LocationModel} from './location.model';
import {Router} from '@angular/router';

@Component({
  templateUrl: './location.component.html',
  styleUrls: ['./location.component.css']
})
export class LocationComponent implements OnInit {

  locations: LocationModel[];
  location: LocationModel;

  constructor(private router: Router) {
    this.locations = [
      {id: 1, name: 'Loc1'},
      {id: 2, name: 'Loc2'},
      {id: 3, name: 'Loc3'},
      {id: 4, name: 'Loc4'}
    ];
  }

  ngOnInit() {
  }

  onSelect(loc: LocationModel): void {
    this.location = loc;
    console.log('onSelect for loc.id[' + this.location.id + ']');
    window.alert('onSelect for loc.id[' + this.location.id + ']');
    this.router.navigate(['/items', this.location.id]);
  }
}

位置 - HTML

<nav class="navbar navbar-fixed-top">
  <a class="navbar-brand" href="do_nothing">CO_ICON</a>

  <div class="nav navbar-nav">
    <a class="nav-item nav-link active" href="do_nothing">
      <div>Add</div>
    </a>
  </div>
</nav>

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <!-- /.card -->
      <div class="card" *ngFor="let location of locations" (click)="onSelect(location)">
        <div class="card-block">
          <div class="list-group">
            <a href="do_nothing" class="list-group-item">{{location.name}}</a>
          </div>
        </div>
      </div>
      <!-- /.card -->
    </div>
  </div>

项目组件

import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import {ItemModel} from './item.model';
// import {ItemService} from './item.service';
import {ActivatedRoute, ParamMap} from '@angular/router';

@Component({
  templateUrl: './items.component.html',
  styleUrls: ['./items.component.css']
})
export class ItemsComponent implements OnInit {

  items: ItemModel[];

  constructor(
    // private itemService: ItemService,
    private route: ActivatedRoute) {
    console.log('constructed ItemsComponent');
    // window.alert('constructed ItemsComponent');
    this.items = [
      {id: 1, location: 1, name: 'Item 1', quantity: 1},
      {id: 2, location: 1, name: 'Item 2', quantity: 2},
      {id: 3, location: 2, name: 'Item 3', quantity: 1},
      {id: 4, location: 1, name: 'Item 4', quantity: 2}
    ];
  }

  ngOnInit() {
    // this.route.paramMap
    //   .switchMap((params: ParamMap) => this.itemService.getItems(+params.get('id')))
    //   .subscribe(list => this.items = list);

    console.log('ngOnInit for is with 1 - A');
    window.alert('ngOnInit for is with 1 - A');
  }

}

项目 - HTML

<nav class="navbar navbar-fixed-top">
  <a class="navbar-brand" href="do_nothing">CO_ICON</a>

  <div class="nav navbar-nav">
    <a class="nav-item nav-link active" href="do_nothing">
      Add Item
    </a>
  </div>
</nav>

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <!-- /.card -->
      <div class="card" *ngFor="let item of items">
        <div class="card-block">
          <div class="list-group">
            <a href="do_nothing" class="list-group-item">{{item.name}}</a>
          </div>
        </div>
      </div>
      <!-- /.card -->
    </div>
  </div>

非常感谢任何帮助!谢谢!

克里斯....

【问题讨论】:

  • 尝试删除 href="#" 这会导致应用重新加载。如果您想要哈希,请尝试使用 angulars 片段指令
  • 啊——这就是问题的症结所在。我在任何地方都没有#。 Angular 会自动执行此操作。考虑从哪里删除它或是否有配置指令要设置?
  • 您的锚标记中没有像您发布的 HTML 那样的 href="#"?
  • 谢谢 - 你完全正确!那行得通。

标签: angular angular2-routing


【解决方案1】:

从您的锚标记中删除 href="#"。原生 html href 属性会导致 Angular 重新加载。 Angular 导航应该由路由器指令和服务处理。

【讨论】:

【解决方案2】:

正如 LLai 在his answer 中所说,为了防止重新加载,您应该使用来自&lt;a&gt; 标记的href 属性,而是使用AngularRouter 和他的routerLink 指令。 p>

因此,请从您的 &lt;a&gt; 标签中删除 href 属性 像这样使用routerLink

<a class="list-group-item" [routerLink]="['login']"></a>

干杯。 :)

【讨论】:

    【解决方案3】:

    添加 javascript:;改为链接href...这不会使页面立即滚动到顶部,或者如果您也使用哈希值,则不会炸毁您的路由器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多