如何导航(重定向)到 Angular 中的另一个页面?

Ani*_*hKr 2 html typescript angular

单击按钮时我试图重定向到另一个页面(Page-II),但不幸的是另一个页面组件加载在同一页面(Page-I)上。到目前为止我尝试过的:

应用程序组件.html

<button (click)="register()"
  mat-raised-button class="searchButton" type="button">Register</button>
  <button (click)="profile()"
  mat-raised-button class="searchButton" type="button">Profile</button>
 <router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

应用程序路由.module.ts

const routes: Routes = [{
  path : 'register',
  component : RegisterComponent
},
{
  path : 'profile',
  component : ProfileComponent
}];

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

export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

应用程序组件.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';
  constructor(private router: Router ) {}

    register = function () {
      location.pathname = ('/register');
    };
    profile = function(){
      this.router.navigateByUrl('/profile');
    };
  ngOnInit() {

  }
}
Run Code Online (Sandbox Code Playgroud)

注意:我知道配置文件加载在同一页面上,但当单击另一个页面上的注册按钮时尝试重定向 register.html 。

Aka*_*wal 5

你能试试这个吗:

register() {
    this.router.navigate(['/register']);
}
Run Code Online (Sandbox Code Playgroud)