我的按钮将在当前页面下方加载一个页面

时间:2019-04-24 14:20:17

标签: html angular

我已经在Angular组件内的html文件中创建了一个按钮。当我单击该按钮时,它会将页面加载到当前页面下方并制作一个长页面,而不是显示新页面。我该如何解决?

<button type="button" class="btn m-1 btn btn-success w-100" (click)="btnClick();"><i class="fa fa-plus"></i>Open page</button>
 btnClick= function () {
    this.router.navigateByUrl('/testcomponent');
  };
<div class="container mt-5">
  <div class="row">
    <div class="col-sm col-md-4">

      <div class="card mb-4">
        <div class="card-body">
          <h5 class="card-title text-center">OPTIONS</h5>
          <p class="card-text">

            <button type="button" class="btn m-1 btn btn-success w-100" (click)="btnClick();"><i class="fa fa-plus"></i>Open page</button>



          </p>
        </div>
      </div>
    </div>



import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { TestComponent} from './testcomponent/testcomponent.component';
import { RouterModule, Routes } from '@angular/router';

const appRoutes: Routes = [
  { path: 'testcomponent', component: TestComponent },
];

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    TestComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  providers: [],
  bootstrap: [AppComponent]


})
export class AppModule { }

1 个答案:

答案 0 :(得分:0)

<a [routerLink]="['/testcomponent']">Open page</a>
<a href="/testcomponent">Open page</a>
了解更多信息 请参阅此https://angular.io/guide/router