sas*_*asi 5 html url routing angular angular6
我试图将我的页面从登录重定向到另一个页面。我遵循此代码。
我的登录组件ts文件:
import { Router } from '@angular/router';
constructor(private router: Router) {
}
funLogin(mobilenumber){
this.router.navigateByUrl('registration');
}
Run Code Online (Sandbox Code Playgroud)
在我的html中,我在Submit btn中调用此函数,
<button class="common-btn btn" (click)="funLogin(mobileNo.value)">Submit</button>
In my app.login.routing file,
export const loginRouting: Routes = [
{
path: '', component: DashboardRootComponent, canActivateChild: [],
children: [
{ path: '', component: DashboardComponent, pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'registration', component: RegistrationComponent },
]
}
]
Run Code Online (Sandbox Code Playgroud)
我已经尝试过“ this.router.navigate”和链接的referlot。但是它没有用。任何人都可以告诉我我哪里出错了,或者如果您可以给我一个工作样本,那会更好。
@sasi ..像这样尝试,
<a routerLink="/registration"><button class="btn btn-success" > Submit </button></a>
Run Code Online (Sandbox Code Playgroud)
更新:
为了在您的应用程序中使用路由,您必须注册允许角度路由器渲染视图的组件。
我们需要在其中App Module
或其中的任何Feature Module
一个(您当前的工作模块)中注册组件,以便路由到特定的组件视图。
我们可以通过两种方式注册组件
.forRoot(appRoutes)
用于应用级别组件的注册,例如
featuteModules
(例如UserManagement),components
并且您希望在上注册root level
。.forChild(featureRoutes)
用于功能模块child components
(例如UserDelete,UserUpdate)。
您可以注册以下内容,
const appRoutes: Routes = [
{ path: 'user', loadChildren: './user/user.module#UserModule' },
{ path: 'heroes', component: HeroListComponent },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(
appRoutes
)
],
Run Code Online (Sandbox Code Playgroud)PS:为了从一个组件导航到另一个组件,您必须
RouterModule
在@angular/router
包中的相应Module Imports数组中包括。
您可以通过两种方式在Angular中将页面导航到另一页面。(两者在包装器级别上都是相同的,但是从我们这边的实现有点不同。)
routerLink
指令为您提供与类类似navigateByUrl()
的绝对路径匹配Router
。
<a [routerLink]=['/registration']><button class="btn btn-success" > Submit </button></a>
Run Code Online (Sandbox Code Playgroud)
如果dynamic values
用于生成链接,则可以传递array
路径段的,然后传递params
每个段的。
例如,routerLink=['/team', teamId, 'user', userName, {details: true}]
意味着我们要生成到的链接/team/11/user/bob;details=true
。
在使用时,有一些有用的要记住的地方routerLink
。
/
,则路由器将从应用程序的根目录查找路由。./
或不以斜杠开头,则路由器将查找当前已激活路由的子级。../
,则路由器将上升一个级别。欲了解更多信息,请看这里。 routerLink
我们需要将Router
类注入组件才能使用其方法。
有两种以上的导航方法,如navigate()
,navigateByUrl()
和其他..,但我们将主要使用这两种方法。
根据提供的命令数组和起点进行导航。如果未提供起始路线,则导航是绝对的。
this.route.navigate(['/team/113/user/ganesh']);
Run Code Online (Sandbox Code Playgroud)
navigate()
命令将最新的字符串追加到现有URL的后面。我们也可以queryParams
像下面这样从这个方法中解析
this.router.navigate(['/team/'], {
queryParams: { userId: this.userId, userName: this.userName }
});
Run Code Online (Sandbox Code Playgroud)
您可以ActivatedRoute
在导航的Component中获得这些值。您可以点击这里详细了解paramMap
,snapshot(no-observable alternative)
。
根据提供的URL进行导航,该URL必须是绝对的。
this.route.navigateByUrl(['/team/113/user/ganesh']);
Run Code Online (Sandbox Code Playgroud)
navigateByUrl()
类似于直接更改位置栏-我们将提供完整的新URL。
小智 8
我正在使用angular 7,并以此方式解决了这个问题。
1.首先我们需要将此模块实现到我们的app.module.ts文件中
import { AppRoutingModule} from './app-routing.module';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
],
})
Run Code Online (Sandbox Code Playgroud)
2.然后打开your.component.html文件,然后触发一种方法来导航到您想去的地方
<button class="btn btn-primary" (click)="gotoHome()">Home</button>
Run Code Online (Sandbox Code Playgroud)
3.然后将your.component.ts文件转到要导航的位置。并在其中添加此代码。
import { Router } from '@angular/router';
export class YourComponentClassName implements OnInit {
constructor(private router: Router) {}
gotoHome(){
this.router.navigate(['/home']); // define your component where you want to go
}
}
Run Code Online (Sandbox Code Playgroud)
4,最后要说的是要小心您的app-routing.module.ts文件,您必须在其中找到要导航的组件路径,否则它将给您错误。就我而言。
const routes: Routes = [
{ path:'', component:LoginComponent},
{ path: 'home', component:HomeComponent }, // you must add your component here
{ path: '**', component:PageNotFoundComponent }
];
Run Code Online (Sandbox Code Playgroud)
谢谢,我想我将分享此路由部分的所有情况。快乐编码!
归档时间: |
|
查看次数: |
34716 次 |
最近记录: |