首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular routing教程问题

Angular routing教程问题
EN

Stack Overflow用户
提问于 2020-02-05 15:03:09
回答 2查看 292关注 0票数 1

我正在做这个非常基础的Angular教程,我在教程的路由部分。单击产品名称时,应该会打开一个包含产品详细信息的页面。但是,它只显示文本"Product Details“。为什么不动呢?

这就是我所看到的:

这是app-module.ts的内容

代码语言:javascript
复制
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { ReactiveFormsModule } from "@angular/forms";

import { AppComponent } from "./app.component";
import { TopBarComponent } from "./top-bar/top-bar.component";
import { ProductListComponent } from "./product-list/product-list.component";
import { ProductAlertsComponent } from "./product-alerts/product-alerts.component";
import { ProductDetailsComponent } from "./product-details/product-details.component";

@NgModule({
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    RouterModule.forRoot([
      { path: "", component: ProductListComponent },
      { path: "products/:productId", component: ProductDetailsComponent }
    ])
  ],
  declarations: [
    AppComponent,
    TopBarComponent,
    ProductListComponent,
    ProductAlertsComponent,
    ProductDetailsComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

这是Product-list.component.html

代码语言:javascript
复制
<h2>Products</h2>

<div *ngFor="let product of products">
  <h3>
    <a
      [title]="product.name + ' details'"
      [routerLink]="['/products', productId]"
    >
      {{ product.name }}
    </a>
  </h3>

  <p *ngIf="product.description">Description: {{ product.description }}</p>

  <button (click)="share()">
    Share
  </button>

  <app-product-alerts [product]="product" (notify)="onNotify()">
  </app-product-alerts>

  <app-product-alerts [product]="product"> </app-product-alerts>
</div>

这是product-list.component.ts

代码语言:javascript
复制
import { Component } from "@angular/core";

import { products } from "../products";

@Component({
  selector: "app-product-list",
  templateUrl: "./product-list.component.html",
  styleUrls: ["./product-list.component.css"]
})
export class ProductListComponent {
  products = products;

  share() {
    window.alert("The product has been shared!");
  }

  onNotify() {
    window.alert("You will be notified when the product goes on sale");
  }
}

我遗漏了什么?在https://angular.io/start/routing上,说明非常简单

Product-details.component.ts

代码语言:javascript
复制
import { Component, OnInit } from "@angular/core"; 
import { ActivatedRoute } from "@angular/router"; 
import { products } from "../products"; 

@Component
 ({ selector: "app-product-details",
    templateUrl: "./product-details.component.html", 
    styleUrls: ["./product-details.component.css"] }) 


export class ProductDetailsComponent implements OnInit { 
           product; 
constructor(private route: ActivatedRoute) {} 

    ngOnInit() { 
       this.route.paramMap.subscribe(
                          params => { this.product = 
                                 products[+params.get("productId")]; }); 
             } }

Product-details.component.html:

代码语言:javascript
复制
<h2>Product Details</h2>

<div *ngIf="product">
  <h3>{{ product.name }}</h3>
  <h4>{{ product.price | currency }}</h4>
  <p>{{ product.description }}</p>
</div>

App-module.ts:

代码语言:javascript
复制
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { ReactiveFormsModule } from "@angular/forms";

import { AppComponent } from "./app.component";
import { TopBarComponent } from "./top-bar/top-bar.component";
import { ProductListComponent } from "./product-list/product-list.component";
import { ProductAlertsComponent } from "./product-alerts/product-alerts.component";
import { ProductDetailsComponent } from "./product-details/product-details.component";

@NgModule({
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    RouterModule.forRoot([
      { path: "", component: ProductListComponent },
      { path: "products/:productId", component: ProductDetailsComponent }
    ])
  ],
  declarations: [
    AppComponent,
    TopBarComponent,
    ProductListComponent,
    ProductAlertsComponent,
    ProductDetailsComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}
EN

回答 2

Stack Overflow用户

发布于 2020-02-05 16:18:41

您看不到其余的产品详细信息,因为您的模板中未定义productId

为简单起见,在本教程中,他将每个产品的索引考虑为productId

因此,在Product-list.component.html中,structural指令应该像这样使用:

代码语言:javascript
复制
<div *ngFor="let product of products; index as productId">

index as productId部件会将索引值分配给productId变量,以便您可以在routerLink属性中使用它。

票数 1
EN

Stack Overflow用户

发布于 2020-12-29 20:48:56

我是个新手,但我想我已经弄明白了。看看教程中显示的这行代码:

代码语言:javascript
复制
   <a [title]="product.name + ' details'" [routerLink]="['/products', product.id]">

假设product.id是从app/products.ts导出的对象数组中的字段。你会注意到我们没有这样的字段,所以你只需要像这样添加它:

代码语言:javascript
复制
export const products = [
  {
    name: 'Phone XL',
    price: 799,
    description: 'A large phone with one of the best screens',
    id: 'thisisID1', // this is added
  },
  {
    name: 'Phone Mini',
    price: 699,
    description: 'A great phone with one of the best cameras',
    id: 'thisisID2', // this is added
  },
  {
    name: 'Phone Standard',
    price: 299,
    description: '',
    id: 'thisisID3', // this is added
  }
];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60070479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档