在路由链接中传递对象 Angular

问题描述

大家好,我想知道当我点击链接时如何在路由中传递一个对象,例如,在我的例子中,我有一个包含产品的页面,如果我点击“购买”,那么将被重定向到我的组件购买,传递完整的产品对象

HomeComponent

<div class="container main-container
    <ul  class="nav product-list">
        <li *ngFor="let products of product" >
            <div class="card">
                <img src="{{products.Image}}" alt="Denim Jeans" style="width:100%;height:250px;">
                <h1>{{products.name}}</h1>
                <p class="price">€ {{products.price}} </p>
                <p><a type="button" class="btn btn-info botaoProduto" [routerLink]="['/shop/purchase',objecProducttHere]">Purchase</a></p>
            </div>
        </li>
    </ul> 
</div>

<script>
</script>

在我的 ShopRoute.ts 中

const shopRoutingConfig:Routes = [
    {
        path:'',component:ShopAppComponent,children:[
            {path:'purchase/objectproduct',component:PurchaseCompoet}
        ]
    }
]
    
@NgModule({
    imports:[
        RouterModule.forChild(shopRoutingConfig)
    ],exports:[RouterModule],})
export class ShopRoutingModule{}

更新

product:Product;
tempData:BasicproductData;

ngOnInit() {

   if(this.obterUsuarioLogado())
    {
        this.router.events.pipe(
            filter(e => e instanceof NavigationStart),map(() => this.router.getCurrentNavigation().extras.state)
          ).subscribe(result=>{ 

              this.product = result as Product //here product object its fine

              this.tempData.id = this.product.id; // here,even product object filled,tempData receive undefinned
              this.tempData.name = this.product.name;//same here problem here
              console.log(this.tempData);

              console.log( this.product);
             
          },error=> this.errorMessage);

          //this.sendOrder(this.tempData);
    };
}

我在 basicproductdata.ts 中的模型 BasicProductData

export class BasicProductData
{
    id:strring;
    name:string;
}

解决方法

你可以在路由中添加对象

function to_normalised_float(slv: std_logic_vector) return half_float is
variable temp_hf: half_float;
begin
    assert slv'high = 15 report "SLV provided is incorrect length for an FP16";
    -- ConstructFloat
    return normalise_float(temp_hf);
end function;

在购买组件

Assert.Throws(function)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...