如何通过单击按钮重定向到 Angular 4 中的新页面?
How to redirect to a new page in Angular 4 through button click?
在 home.component.html
页面中,我希望按钮在单击时将 url 重定向到新页面。
我希望当我单击该按钮时,当前 url http://localhost:24282/home
将重定向到此 url http://localhost:24282/master
和 master.component.html
页面显示。
问题是,虽然 url 在我单击按钮时被重定向到 http://localhost:24282/master
,但我仍然在当前页面 home.component.html
。我没有完全重定向到主页 master.component.html
下面是实现。
app.routing.ts
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './Components/home.component';
import { MasterComponent } from './Components/master/master.component';
import { PageNotFoundComponent } from "./Components/common/page-not-found.component";
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'master', component: MasterComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
app.module.ts
import { NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { HomeComponent } from './Components/home.component';
import { MasterComponent } from './Components/master/master.component';
import { PageNotFoundComponent } from "./Components/common/page-not-found.component";
@NgModule({
imports: [BrowserModule, routing],
declarations: [AppComponent, HomeComponent, MasterComponent, PageNotFoundComponent],
providers: [{ provide: APP_BASE_HREF, useValue: '/' }],
bootstrap: [AppComponent]
})
export class AppModule {}
app.component.ts - 定义 newChange() 方法的地方
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'moc-landing',
templateUrl: './app/Components/home.component.html',
styleUrls: ['./app/Components/home.component.css']
})
export class AppComponent {
constructor(private router: Router) {}
newChange(): void {
this.router.navigateByUrl('master');
}
}
home.component.html
<button id="new" class="btn btn-primary" (click)="newChange()">New Change Request</button>
我不确定我是否正确使用了router.navigateByUrl
或者有其他方法吗?
在 ASP.NET MVC 中,使用 razor 的 URL 助手 类(例如 @Url.Action
)很容易做到这一点。
但是在 Angular 4(我对它很陌生)中,我不知道如何实现它。我已经进行了一些研究,但找不到与此相关的任何内容。
非常感谢任何帮助。
navigateByUrl
总是期望绝对 URL.
i.e. router.navigateByUrl("/team/33/user/11");
如果要提供相对路径,可以使用navigate
代替:
router.navigate(['team', 33, 'user', 11], {relativeTo: route});
试试这个
<button (click)="router.navigate(['/master']);">
<span>Go to master Page</span>
</button>
或
在你的 home.html
<button (click)="goToPage('master')">
<span>Go to master Page</span>
</button>
并在您的主页组件中
import { Router } from '@angular/router';
constructor(private router: Router){
}
function goToPage(pageName:string){
this.router.navigate([`${pageName}`]);
}
我同意 Veena K. Suresh 的解决方案,即通过点击事件进行导航,但您也可以通过相同的技巧传递数据
- 将
router
模块导入到您当前的组件中,例如:
import { Router } from '@angular/router';
- 将它注入你的构造函数
constructor(
private router: Router
) { }
- 刚刚添加到您希望在其上使用的锚点
<a (click)="router.navigate(['/search',search.value]);" class="search-area__icon">
<i class="la la-search"></i>
</a>
回复晚了,但还是把答案贴出来,希望我的回答对其他人有用。
修改 app.component.html
文件中的代码,如下所示。
<div>
<router-outlet></router-outlet>
</div>
如果您使用的是 link (<a>
),您只需要 routerLink directive/parameter:
<a class="btn" routerLink="/votes">Check votes</a>
如果您使用的是按钮 (<button>
),则需要 (click)
事件:
<button class="btn" (click)="goToVotes()">Check votes</button>
在这种情况下,最好创建一个 function/method,因为在组件的构造函数中保留私有参数 是一个好的做法,即不要使用router.navigate() 直接在您的 HTML 上(避免 'router' 由于使用组件的私有成员而引起的警告)。
import { Component } from '@angular/core';
import { Router } from '@angular/router';
// component details here...
export class MyComponent {
constructor(private router: Router){ }
goToVotes($myParam: string = ''): void {
const navigationDetails: string[] = ['/votes'];
if($myParam.length) {
navigationDetails.push($myParam);
}
this.router.navigate(navigationDetails);
}
}
上面的 goToVotes 方法也会向 navigate 的数组添加第二个值,如果它的参数不为空的话,因此:
<button class="btn" (click)="goToVotes('ca')">Go vote</button>
将重定向到 /votes/ca
。
在 home.component.html
页面中,我希望按钮在单击时将 url 重定向到新页面。
我希望当我单击该按钮时,当前 url http://localhost:24282/home
将重定向到此 url http://localhost:24282/master
和 master.component.html
页面显示。
问题是,虽然 url 在我单击按钮时被重定向到 http://localhost:24282/master
,但我仍然在当前页面 home.component.html
。我没有完全重定向到主页 master.component.html
下面是实现。
app.routing.ts
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './Components/home.component';
import { MasterComponent } from './Components/master/master.component';
import { PageNotFoundComponent } from "./Components/common/page-not-found.component";
const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'master', component: MasterComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
app.module.ts
import { NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { HomeComponent } from './Components/home.component';
import { MasterComponent } from './Components/master/master.component';
import { PageNotFoundComponent } from "./Components/common/page-not-found.component";
@NgModule({
imports: [BrowserModule, routing],
declarations: [AppComponent, HomeComponent, MasterComponent, PageNotFoundComponent],
providers: [{ provide: APP_BASE_HREF, useValue: '/' }],
bootstrap: [AppComponent]
})
export class AppModule {}
app.component.ts - 定义 newChange() 方法的地方
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'moc-landing',
templateUrl: './app/Components/home.component.html',
styleUrls: ['./app/Components/home.component.css']
})
export class AppComponent {
constructor(private router: Router) {}
newChange(): void {
this.router.navigateByUrl('master');
}
}
home.component.html
<button id="new" class="btn btn-primary" (click)="newChange()">New Change Request</button>
我不确定我是否正确使用了router.navigateByUrl
或者有其他方法吗?
在 ASP.NET MVC 中,使用 razor 的 URL 助手 类(例如 @Url.Action
)很容易做到这一点。
但是在 Angular 4(我对它很陌生)中,我不知道如何实现它。我已经进行了一些研究,但找不到与此相关的任何内容。
非常感谢任何帮助。
navigateByUrl
总是期望绝对 URL.
i.e. router.navigateByUrl("/team/33/user/11");
如果要提供相对路径,可以使用navigate
代替:
router.navigate(['team', 33, 'user', 11], {relativeTo: route});
试试这个
<button (click)="router.navigate(['/master']);">
<span>Go to master Page</span>
</button>
或
在你的 home.html
<button (click)="goToPage('master')">
<span>Go to master Page</span>
</button>
并在您的主页组件中
import { Router } from '@angular/router';
constructor(private router: Router){
}
function goToPage(pageName:string){
this.router.navigate([`${pageName}`]);
}
我同意 Veena K. Suresh 的解决方案,即通过点击事件进行导航,但您也可以通过相同的技巧传递数据
- 将
router
模块导入到您当前的组件中,例如:
import { Router } from '@angular/router';
- 将它注入你的构造函数
constructor(
private router: Router
) { }
- 刚刚添加到您希望在其上使用的锚点
<a (click)="router.navigate(['/search',search.value]);" class="search-area__icon">
<i class="la la-search"></i>
</a>
回复晚了,但还是把答案贴出来,希望我的回答对其他人有用。
修改 app.component.html
文件中的代码,如下所示。
<div>
<router-outlet></router-outlet>
</div>
如果您使用的是 link (<a>
),您只需要 routerLink directive/parameter:
<a class="btn" routerLink="/votes">Check votes</a>
如果您使用的是按钮 (<button>
),则需要 (click)
事件:
<button class="btn" (click)="goToVotes()">Check votes</button>
在这种情况下,最好创建一个 function/method,因为在组件的构造函数中保留私有参数 是一个好的做法,即不要使用router.navigate() 直接在您的 HTML 上(避免 'router' 由于使用组件的私有成员而引起的警告)。
import { Component } from '@angular/core';
import { Router } from '@angular/router';
// component details here...
export class MyComponent {
constructor(private router: Router){ }
goToVotes($myParam: string = ''): void {
const navigationDetails: string[] = ['/votes'];
if($myParam.length) {
navigationDetails.push($myParam);
}
this.router.navigate(navigationDetails);
}
}
上面的 goToVotes 方法也会向 navigate 的数组添加第二个值,如果它的参数不为空的话,因此:
<button class="btn" (click)="goToVotes('ca')">Go vote</button>
将重定向到 /votes/ca
。