简体   繁体   English

在 Angular 中重新加载页面时出现错误

[英]getting error when reloading page in Angular

i have a list of decks, when i click on a deck the proper deck-detail component is loaded showing the details of the deck selected and the Url becomes deck/id/deckName.我有一个卡组列表,当我单击卡组时,会加载正确的卡组详细信息组件,显示所选卡组的详细信息,并且 Url 变为卡组/id/卡组名称。 But when i reload the page or if i try to copy the url of a specific deck i get this error:但是,当我重新加载页面或尝试复制特定牌组的 url 时,我会收到此错误:

Cannot read property 'deckName' of undefined无法读取未定义的属性“deckName”

i think i'm doing something wrong in the routing, this is deck-details component我想我在路由中做错了什么,这是甲板细节组件

import { Card } from "./../../card/card.model";
import { Deck } from "./../../deck/deck.model";
import { Component, OnInit, Input } from "@angular/core";
import { DeckService } from "src/app/deck/deck.service";
import { ActivatedRoute, Params, Router } from "@angular/router";
import { Subscription } from "rxjs";

@Component({
  selector: "app-deck-details",
  templateUrl: "./deck-details.component.html",
  styleUrls: ["./deck-details.component.scss"],
})
export class DeckDetailsComponent implements OnInit {
  //@Input() deck: Deck;
  paramsSubscription: Subscription;
  id: number;
  decks: Deck[];
  deck: Deck;

  constructor(private deckService: DeckService, private route: ActivatedRoute) {

  }

  ngOnInit() {

    this.id = this.route.snapshot.params["id"];
    this.paramsSubscription = this.route.params.subscribe((params: Params) => {
      this.id = params["id"];
      this.decks = this.deckService.getDecks();
      this.deck = this.decks.find (
        deck => deck.id === this.id
      );

    });


  }

    ngOnDestroy() { this.paramsSubscription.unsubscribe(); }


}

and html和 html

<div class="container">
  <div class="row">
    <div class="deck-details  " style="border:solid black">
      <h1>{{ deck.deckName }} : {{ deck.deckClass }}</h1>
      <div *ngFor="let card of deck.deckCards">
        <span
      [ngClass]="{
        common: card.rarity == 'common',
        rare: card.rarity == 'rare',
        epic: card.rarity == 'epic',
        legendary: card.rarity == 'legendary'
      }"
    >
      {{ card.name }}
    </span>
    <h4 class="inlineh4">: {{ card.manaCost }} mana</h4>
        <img [src]="card.imagePath" alt="" style="height: 20px;" />
      </div>
    </div>
  </div>
</div>

this is the app routing module这是应用程序路由模块

const appRoutes: Routes = [
  { path: '', component: CardListComponent },
  { path: 'decks', component: DeckListComponent, children: [
    { path: ':id/:deckName', component: DeckDetailsComponent }
  ] },
  { path: 'createDeck', component: CreateDeckComponent },
];
@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

In your html file add a condition if deck is available or not.在您的 html 文件中添加一个条件,如果甲板可用或不可用。

<div class="container" *ngIf="deck">

as this is undefined when page load因为这是在页面加载时未定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改路线参数时的角度重新加载页面 - Angular reloading page when changing route param 重新加载页面时,生产URL中的Angular 2断开 - Angular 2 in production URLs are breaking when reloading the page 页面重载时如何不重载组件 Angular - How not to reload component when page is reloading Angular 当我尝试提交登录表单时,页面正在重新加载,没有获取表单组 -Ionic,Angular 的值 - When I try to submit the login form the page is reloading , not getting the values of form group -Ionic ,Angular 重新加载 Commerce.js 结帐页面时出错(反应教程) - Getting error when reloading the Commerce.js Checkout page (react tutorial) Angular 2 页面/组件未重新加载 - Angular 2 Page / Component not reloading 在不刷新页面的情况下重新加载 matplotlib plot 时出现错误 - Getting Error while reloading the matplotlib plot without refreshing the page Angular无限滚动:重新加载页面 - Angular infinite scrolling: reloading page 为什么 Angular 表单会重新加载页面? - Why is Angular form reloading the page? 使用Angular.js和.htaccess重新加载页面时未获取JS / CSS文件链接 - Not getting JS/CSS filelinks while reloading the page using Angular.js and .htaccess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM