我刚刚开始使用角度4而不是angular.js 1.
我已经按照英雄教程了解了角度4的基本原理,我目前正在使用角度自己的"@ angular/router"包中的"RouterModule".
为了实现我的应用程序的一些授权我想知道如何手动重定向到另一条路线,我似乎无法在互联网上找到任何有用的信息.
Bou*_*ule 184
首先,您需要导入角度路由器:
import {Router} from "@angular/router"
Run Code Online (Sandbox Code Playgroud)
然后将其注入组件构造函数中:
constructor(private router: Router) { }
Run Code Online (Sandbox Code Playgroud)
最后打电话给..navigate方法在任何需要"重定向"的方法:
this.router.navigate(['/your-path'])
Run Code Online (Sandbox Code Playgroud)
您还可以在路线上设置一些参数,例如user/5:
this.router.navigate(['/user', 5])
Run Code Online (Sandbox Code Playgroud)
文档:Angular官方文档
angularjs中的重定向4按钮用于例如:app.home.html中的事件
<input type="button" value="clear" (click)="onSubmit()"/>
Run Code Online (Sandbox Code Playgroud)
并在home.componant.ts
import {Component} from '@angular/core';
import {Router} from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.home.html',
})
export class HomeComponant {
title = 'Home';
constructor(
private router: Router,
) {}
onSubmit() {
this.router.navigate(['/doctor'])
}
}
Run Code Online (Sandbox Code Playgroud)
您应该像这样在构造函数中注入 Router;
constructor(private router: Router) { }
Run Code Online (Sandbox Code Playgroud)
然后你可以在任何你想要的地方做这件事;
this.router.navigate(['/product-list']);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95297 次 |
| 最近记录: |