javascript Angular 4:点击带有参数的链接

xe55xuns  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(81)

我对Angular 4还是个新手。我有一个锚标记,点击它应该重定向到一个链接,我需要传递参数以及我。我不知道我的方法是对还是错。但更重要的是,我如何传递参数?我需要指导。

form.component.html:

<a (click)="myFunc()" href="" target="_blank">Go to mymodule</a><br />

form.component.ts

myFunc() {
  console.log("function called");
  window.open('../myURL/mypage.jsp?rurl=' + encodeURIComponent($('display').attr('href')));
  }
vybvopom

vybvopom1#

IMHO,你应该使用Angular router这样导航:

myFunc() {
  console.log("function called");
  this.router.navigate(['../myURL'], {queryParams:{rurl: "someId"}})
 }

另外,不要忘记导入Router,如下所示:

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

this.router = Router;

**注意:**在URL前使用../会使路由器上升一级。

2ic8powd

2ic8powd2#

如果您想在应用程序外重定向:

myFunc(){
   window.location.href = `full_url?rurl=${encodeURIComponent(display)})`;
}

如果您想在应用程序中导航到页面:

myFunc(){
this.router.navigate(['/navigation_route'], {queryParams:{returnRoute: 'any_path'});
}
wi3ka0sx

wi3ka0sx3#

使用[routerLink]:

<a [routerLink]="['/route-name', id]" href="">Go to mymodule</a>

另外,你不应该忘记改变路由配置,如:

[path: 'route-name/:id', component:ComponentName, pathMatch: 'full']

使用navigateByUrl:

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

constructor(private router: Router) { }

myFunc = function () {
        this.router.navigateByUrl('/route-name', {queryParams:{param-key: param-value}});
};

相关问题