我正在开发角度应用程序。我想使用服务将对象数组从一个组件转移到另一个组件。我正在使用以下链接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)