gpt4 book ai didi

javascript - 使用路由将 JSON 从一个组件发送到另一个组件而不显示在 URL 上?

转载 作者:行者123 更新时间:2023-12-03 01:45:54 24 4
gpt4 key购买 nike

我正在尝试在路由 URL 上传递 JSON,这是我的

app.route.ts代码

 {path: 'calculator', component: CalculatorComponent,data : {some_data : null}},

我的路由数据的代码是

 this.router.navigate(['/home/calculator',  {some_data:this.loanData}]);

而calculator.ts有oninit方法代码是这样的

import {ActivatedRoute} from '@angular/router';

constructor(private route: ActivatedRoute) {}
sub
ngOnInit() {
this.sub = this.route
.data
.subscribe(v => console.log(v));
}

输出是这样的

{"some_data":null}

问题是它没有显示 json,我从 ts 第一个组件传递

最佳答案

    Is there a reason you want to navigate by url why not use the navigate method like this:

in RouteConf: { path: '/path/:data', name: 'Defined_Path_Name', component: PathComponent }
navigate with: this.router.navigate(['Defined_Path_Name', { data: { entity: 'entity' } } ]);

in /path: console.log(this.routeParams.get('data'))
that got me: Object {entity: "entity"}

//in your route file

in RouteConf: {path: 'calculator/:data', component: CalculatorComponent},

//in your component file navigate by like below

navigate with: this.router.navigate(['/calculator', { data: this.loaddata } ]);

//and get data in /calculator path like

this.data = this.route.snapshot.data['data'];

than console.log(this.data);

关于javascript - 使用路由将 JSON 从一个组件发送到另一个组件而不显示在 URL 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com