Angular 如何使用从一个组件到另一个组件的值
我有两个组件Angular 如何使用从一个组件到另一个组件的值,angular,typescript,nativescript,Angular,Typescript,Nativescript,我有两个组件 export class ResetPassIdComponent implements OnInit { constructor() {} ngOnInit(){ this.resetPasswordForm = this.fb.group({ 'password': new FormControl('', Validators.required), 'myid': new FormControl('', Val
export class ResetPassIdComponent implements OnInit {
constructor() {}
ngOnInit(){
this.resetPasswordForm = this.fb.group({
'password': new FormControl('', Validators.required),
'myid': new FormControl('', Validators.required)
});
}
onReset() {
console.log(this.resetPasswordForm.value)
}
}
第一:component.ts
ts
.html
路由选择
{ path: 'resetPasswordRequest', component: ResetPassIdComponent }
你知道如何在ResetPassIdComponent中获取这个.myid吗
谢谢您既可以使用服务,也可以通过路线传递服务
this.router.navigate(['/test/resetPasswordRequest', this.myid]);
并从另一个组件解析它
export class ResetPassIdComponent implements OnInit {
constructor(private router:Router) {}
ngOnInit(){
let param = this.router.routerState.root.queryParams
}
}
您可以使用服务,也可以通过路由传递服务
this.router.navigate(['/test/resetPasswordRequest', this.myid]);
并从另一个组件解析它
export class ResetPassIdComponent implements OnInit {
constructor(private router:Router) {}
ngOnInit(){
let param = this.router.routerState.root.queryParams
}
}
您可以使用路由导航传递ID。但是,请确保在路由到其他页面之前获得正确的myId值
this.router.navigate(['/test/resetPasswordRequest', this.myid]);
要在另一个组件中接收,可以使用以下命令:
import { ActivatedRoute, Params } from '@angular/router';
export class ResetPassIdComponent implements OnInit {
id: any
constructor(
private route: ActivatedRoute
) {}
ngOnInit(){
this.route.params.subscribe((params: Params) => { this.id = params['myid']; });
}
}
您可以使用路由导航传递ID。但是,请确保在路由到其他页面之前获得正确的myId值
this.router.navigate(['/test/resetPasswordRequest', this.myid]);
要在另一个组件中接收,可以使用以下命令:
import { ActivatedRoute, Params } from '@angular/router';
export class ResetPassIdComponent implements OnInit {
id: any
constructor(
private route: ActivatedRoute
) {}
ngOnInit(){
this.route.params.subscribe((params: Params) => { this.id = params['myid']; });
}
}
将其存储在服务中或通过路由解析器传递。angularA basic example的文档中描述了这两种解决方案:这里的许多人没有考虑到上面的代码是NativeScript,而不是Angular for WEB。这是NativeScript Angular的主详细信息模板,它显示了如何使用NativeScript的
路由器扩展
另一个NativeScript演示。在这里,您还可以使用如图所示的Angular ActivatedRoute将其存储在服务中或通过路由解析程序传递。angularA basic example的文档中描述了这两种解决方案:这里的许多人没有考虑到上面的代码是NativeScript,而不是Angular for WEB。这是NativeScript Angular的主详细信息模板,它显示了如何使用NativeScript的路由器扩展
另一个NativeScript演示您也可以使用Angular ActivatedRoute,如图所示是,如何从第一个组件获取它,并在第二个组件中使用它myid
?this.router.snapshot.paramMap.get(“id”)
console.log(this.router.snapshot.paramMap.get(“id”))show nullProperty“params”在类型“router”上不存在。ts(2339)使用routerState
表示角度6>是,如何从第一个组件获取它并在第二个组件中使用它myid
?this.router.snapshot.paramMap.get(“id”)
console.log(this.router.snapshot.paramMap.get(“id”))show nullProperty“params”在类型“router”上不存在。ts(2339)使用routerState
用于angular 6>是否可以检查“this.myid”是否在AppComponent中指定了适当的值?同样,在执行此操作之前.router.navigate('PATH')必须在AppComponent中获取“this.myid=url\u id”。请检查“this.myid”在AppComponent中的值是否正确?在执行此操作之前,您必须在AppComponent中获取“this.myid=url\u id”。