【发布时间】:2020-12-24 21:56:37
【问题描述】:
目前我尝试使用 Angular 9 进行 SPA。我使用此路由 localhost:4200/products 制作了一个名为 products 的模块,并使用此路由 http://localhost:4200/products/(number_id_product) 查找指定产品)
我想在每次需要查看产品时实现这个组件。我的问题是,当我使用带有 [routerlink] 的链接(http://localhost:4200/products/1 到 http://localhost:4200/products/2)从当前产品页面转到另一个产品时,url 发生了变化但内容页面不会改变并保持上一个产品的相同信息。
我不知道 [routerlink] 重定向发生了什么
我的代码.ts
import { Component, OnInit } from '@angular/core';
import { Product } from 'src/app/shared/models/product';
import { ActivatedRoute } from '@angular/router';
import { ProductsService } from 'src/app/shared/services/products.service';
@Component({
selector: 'ec-product-description',
templateUrl: './product-description.component.html',
styleUrls: ['./product-description.component.css']
})
export class ProductDescriptionComponent implements OnInit {
id: string;
product: Product
constructor(private route: ActivatedRoute, private service: ProductsService) { }
ngOnInit(): void {
this.id= this.route.snapshot.paramMap.get('id')
//get products/id
this.service.getProduct(this.id)
.subscribe(prod=>{
console.log(`Prod ↓ `)
console.log(prod)
this.product=prod
})
}
}
我的代码 html
<div class="container-product">
<h1>{{product.title}}</h1>
<img mat-card-image [src]="product.thumbImage">
<span class="product-descrp">{{product.description}}</span>
<span [routerLink]="['','products','another_id_product']">See another product!</span>
</div>
【问题讨论】:
标签: angular typescript routerlink