Angular如何使用路由传递数据

4 typescript angular-material angular

我有两个组件如下:

  1. 我在其中显示表格的产品表
  2. 产品消息,我在其中显示点击表格的结果

我让组件与输入和输出进行通信,一切都运行良好。

现在我想尝试通过路由传递数据的新方法。

产品表.html

<!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef>Symbol</th>
    <td mat-cell *matCellDef="let element">{{element.symbol}}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;" 
  (click)="selected(row)" (click)="navigate(row)"></tr> // i try with navigate method here
Run Code Online (Sandbox Code Playgroud)

产品表.ts

export class ProductTableComponent implements OnInit ,OnDestroy{
  
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  public dataSource!: MatTableDataSource<Product>;
  
  private sub: Subscription = new Subscription;
  
  @Output() selectedArrayEnfant2 = new EventEmitter();
  
  constructor(private productService: ProductService, private router: Router) { }
  
  ngOnInit(): void {
    this.getAllProduct();
  }
  
  getAllProduct() {
    let resp = this.productService.getAll();
    this.sub =resp.subscribe(result => {
      this.dataSource =  new MatTableDataSource<Product>();
      this.dataSource.data = result as Product[];
    });
  }
  
  selected(row:Product) {
    this.selectedArrayEnfant2.emit(row);
  }
  
  navigate(row:Product) {
    this.router.navigate(['/msg', row]); // i try something here but this method open the new page instead just pass data in message component via routing
  }
Run Code Online (Sandbox Code Playgroud)

产品消息.ts

export class ProductMessageComponent implements OnInit, OnChanges {

  @Input() public selectedArrayEnfant1!: Product;

  constructor() { }

  ngOnChanges(changes: SimpleChanges): void {}

  ngOnInit(): void {
   
  }
}
      
Run Code Online (Sandbox Code Playgroud)

产品.message.html

<h1>{{selectedArrayEnfant1?.name}}</h1>
Run Code Online (Sandbox Code Playgroud)

我在product.page.html 中显示这两个组件

产品.page.html

<div class="center">
    <app-product-table (selectedArrayEnfant2)="receive($event)"></app-product-table>
    <app-product-message [selectedArrayEnfant1]="selectedArrayParent"></app-product-message>
</div>
Run Code Online (Sandbox Code Playgroud)

应用程序路由模块

const routes: Routes = [
  { 
    path: '', 
    redirectTo: '/hello', pathMatch: 'full' 
  },
  {
    path: 'product',
    loadChildren: () => import('./components/product/module/product.module').then(m => m.ProductModule),
  },
  { 
    path: 'msg',
    component: ProductMessageComponent
  }
];
Run Code Online (Sandbox Code Playgroud)

Sal*_*med 7

如果要传递一个对象(假设row这里是一个对象),则需要将其字符串化为字符串并作为字符串传递。当你得到它之后,你需要再次将它解析成一个对象。

因此,对于导航,请执行以下操作:

this.router.navigate(['/msg', {my_object: JSON.stringify(row)}]);
Run Code Online (Sandbox Code Playgroud)

然后将其解析回接收者(msg)组件中

this.Obj = JSON.parse(this.route.snapshot.paramMap.get('my_object'));
Run Code Online (Sandbox Code Playgroud)

像这样更改构造函数参数:

constructor(
    ...    
    private router: Router,
    private route: ActivatedRoute
    ) { }
Run Code Online (Sandbox Code Playgroud)