Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度-routerLink和状态的问题_Angular_Angular Routing_Routerlink_Angular Routerlink - Fatal编程技术网

Angular 角度-routerLink和状态的问题

Angular 角度-routerLink和状态的问题,angular,angular-routing,routerlink,angular-routerlink,Angular,Angular Routing,Routerlink,Angular Routerlink,我想使用routerLink和state从一个html页面路由到另一个页面。 有了标签就并没有问题了,在登录页面的ngOnInit过程中,我可以按预期检索状态。 使用标记主页也可以导航,但状态结果未定义 我怎么了 登录页面的html 主页的ts 您可以通过单击事件和设置状态导航到所需页面: <button (click)="test()">Test</button> 在目标中,您可以检索以下数据: example:string; constructor(private

我想使用routerLink和state从一个html页面路由到另一个页面。 有了标签就并没有问题了,在登录页面的ngOnInit过程中,我可以按预期检索状态。 使用标记主页也可以导航,但状态结果未定义

我怎么了

登录页面的html

主页的ts


您可以通过单击事件和设置状态导航到所需页面:

<button (click)="test()">Test</button>
在目标中,您可以检索以下数据:

example:string;
constructor(private router: Router) { 
   const navigation = this.router.getCurrentNavigation();
   const state = navigation.extras.state as {example: string};
   this.example = state.example;
}

您可以通过单击事件和设置状态导航到所需页面:

<button (click)="test()">Test</button>
在目标中,您可以检索以下数据:

example:string;
constructor(private router: Router) { 
   const navigation = this.router.getCurrentNavigation();
   const state = navigation.extras.state as {example: string};
   this.example = state.example;
}

我认为不可能通过按钮来传递状态。如果我们检查routerLink的源代码,我们可以看到

当不是标签时:

@指令{选择器:':nota:notarea[routerLink]'} 额外费用中不包括州:

@HostListener'click' onClick:boolean{ 临时演员={ skipLocationChange:AttrboolValue this.skipLocationChange, replaceUrl:attrBoolValuethis.replaceUrl, }; this.router.navigateByUrlthis.urlTree,附加; 返回true; } 然而,当我们有一个a标签时:

@指令{selector:'a[routerLink],area[routerLink]} 它包括:

@HostListener'单击',[/**…**/] onClick/**……**/:布尔值{ // ..... 临时演员={ skipLocationChange:AttrboolValue this.skipLocationChange, replaceUrl:attrBoolValuethis.replaceUrl,
状态:this.state/我认为不可能通过按钮传递状态。如果我们检查routerLink的源代码,我们可以看到

当不是标签时:

@指令{选择器:':nota:notarea[routerLink]'} 额外费用中不包括州:

@HostListener'click' onClick:boolean{ 临时演员={ skipLocationChange:AttrboolValue this.skipLocationChange, replaceUrl:attrBoolValuethis.replaceUrl, }; this.router.navigateByUrlthis.urlTree,附加; 返回true; } 然而,当我们有一个a标签时:

@指令{selector:'a[routerLink],area[routerLink]} 它包括:

@HostListener'单击',[/**…**/] onClick/**..**/:布尔值{ // ..... 临时演员={ skipLocationChange:AttrboolValue this.skipLocationChange, replaceUrl:attrBoolValuethis.replaceUrl,
state:this.state//hi,我没有看到您的组件ts文件中定义了navExtra。对不起,我错了,我不记得粘贴起始页[routerLink]=['/home']][state]=navExtra的ts。state@ErvinLlojku同样的问题也发生在你上次提出的修正中……我也经历过这个问题:8.2.14,看起来像[状态]指令仅用于taghi,我没有在组件ts文件中看到定义的navExtra。对不起,我的错误,我不记得粘贴起始页[routerLink]=['/home'][state]=navExtra的ts。state@ErvinLlojku同样的问题也发生在你上次提出的修正中……我也经历过这个问题:8.2.14,看起来像[状态]指令只与标记一起工作我知道这一点,但我正在寻找一种从html也从标记而不仅仅是fro标记来做这件事的方法。@Xilo-我尝试了很多,但从html也从标记中做不到;上面唯一的方法是有效的。我知道这一点,但我正在寻找一种从html也从标记而不仅仅是fro标记来做这件事的方法。@Xilo-我尝试过很多,但它不能从Html和标记中工作;上面唯一的方法是有效的。
test(){
  const navigationExtras: NavigationExtras = {state: {example: 'This is an example'}};
  this.router.navigate(['test'], navigationExtras);
}
example:string;
constructor(private router: Router) { 
   const navigation = this.router.getCurrentNavigation();
   const state = navigation.extras.state as {example: string};
   this.example = state.example;
}