【问题标题】:How to redirect Page in Angular?如何在Angular中重定向页面?
【发布时间】:2018-10-24 20:45:32
【问题描述】:

我正在尝试将一个组件重定向到另一个组件(例如页面重定向)

Angular link

这是我的代码

app-routing.module.ts

import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
  { path: 'role', component: RoleComponent },

];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

top-header.component.html

 <ul class="navbar-nav mr-auto">
       <li class="nav-item">
          <a class="nav-link" href="#">Verified</a>
        </li>
        <li class="nav-item">
          <a routerLink="/role" class="nav-link" href="#">ROLES</a>
        </li>
      </ul>

AppComponent.html

<div class="container p-md-0">
<app-top-header></app-top-header>
<router-outlet></router-outlet>
<app-main-contain></app-main-contain>
</div>

【问题讨论】:

  • 请正确配置路由器。您没有为/AppComponent提及路由器。
  • 你告诉过这段代码吗? {路径:'AppComponent',组件:AppComponent},。当我把这个代码设计显示多次。
  • 你只需使用 RouterModule.forChild(ROUTES) 来代替 forRoot(routes) 进行延迟加载,请在此处附加 app.module.ts 文件。
  • @NgModule({ 声明:[ AppComponent,TopHeaderComponent,AlertComponent,MainContainComponent,FooterComponent,RoleComponent,],导入:[ BrowserModule,AppRoutingModule ],提供者:[],引导程序:[AppComponent] }) 导出类 AppModule { }
  • 删除 { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },行并检查。

标签: html angular


【解决方案1】:

只需将此路径添加到您的app-routing.module.ts 文件中,以便它知道路径:

{ path: 'AppComponent', component: AppComponent }

【讨论】:

【解决方案2】:

您的 app-routing.module.ts 没问题。现在,在您的组件中,您可以执行 Import Router 并使用它来重定向。

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() { }

  redirect() {
      this.router.navigate(['role']);
  }

}

然后在你的 component.html 中调用 redirect() 函数

<button (click)="redirect()"></button>

【讨论】:

    【解决方案3】:

    你可以使用Router类的router.navigate函数。只需从@angular/router 导入路由器并在constructor(private router: Router) 中为其创建一个对象并使用router.navigate(['/roles']);

    import { RoleComponent } from './role/role.component';
    import { AppComponent } from './app.component';
    import { NgModule }             from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    
    const routes: Routes = [
      { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
      { path: 'role', component: RoleComponent },
    
    ];
    
    @NgModule({
      imports: [ RouterModule.forRoot(routes) ],
      exports: [ RouterModule ]
    })
    export class AppRoutingModule {
      constructor(private router: Router)
    }
    
    functionOnWhichRedirectShouldHappen(){
      router.navigate(['/role']);
    }
    

    HTML

    <ul class="navbar-nav mr-auto">
       <li class="nav-item">
           <a class="nav-link" href="#">Verified</a>
       </li>
       <li class="nav-item">
          <button (click)="functionOnWhichRedirectShouldHappen()">ROLES</button>
       </li>
    </ul>
    

    【讨论】:

    • 在哪个页面?
    • @DhrupalSuthar 现在检查答案
    • 你也可以使用routerLink。像 [routerLink]="['/role']" 一样使用它
    【解决方案4】:

    试试下面的代码

    你可以使用 Angular $window

    $window.location.href = '/index.html';
    

    控制器中的示例用法:

    (function () {
        'use strict';
    
        angular
            .module('app')
            .controller('LoginCtrl', LoginCtrl);
    
        LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];
    
        function LoginCtrl($window, loginSrv, notify) {
            /* jshint validthis:true */
            var vm = this;
            vm.validateUser = function () {
                 loginSrv.validateLogin(vm.username, vm.password).then(function (data) {          
                    if (data.isValidUser) {    
                        $window.location.href = '/index.html';
                    }
                    else
                        alert('Login incorrect');
                });
            }
        }
    })();
    

    【讨论】:

    • 这个问题与 Angular 有关,而不是与您的答案使用的 AngularJS 有关。两者的工作方式截然不同。
    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 2020-07-30
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多