小编Rah*_*ngh的帖子

如何将角度应用程序中的对象数组从一个组件传递到另一个组件

我正在开发角度应用程序。我想使用服务将对象数组从一个组件转移到另一个组件。我正在使用以下链接Pass array of int in Angular Route

通行数据.html

<div>
 <button type="button" [routerLink]="['/receive-data']">Pass Data</button>
</div>
Run Code Online (Sandbox Code Playgroud)

PassData.ts

import ....
@Component({
  selector: 'app-PassData',
  templateUrl: './PassData.component.html',
  styleUrls: ['./PassData.component.css'],
  providers: [DataService]
})

constructor( private dataService: DataService) { }
export class PassData {
  passObjects : any[] = [{'name': 'John', 'city': 'paris'},{'name': 'Bob', 'city': 'london'}, {'name': 'Grim', 'city': 'paris'}];

  passDataToService() {
     this.dataService.storePassedObject(this.passObjects);
  }
    
}
Run Code Online (Sandbox Code Playgroud)

ReceiveData.ts

@Component({
  selector: 'app-ReceiveData',
  templateUrl: './ReceiveData.component.html',
  styleUrls: ['./ReceiveData.component.css'],
  providers: [DataService]
})

export class ReceiveData implements OnInit {
  let selectedProducts = this.DataService.retrievePassedObject();
  console.log(JSON.stringify(selectedProducts)); …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

1
推荐指数
1
解决办法
3583
查看次数

标签 统计

angular ×1

javascript ×1

typescript ×1