我怎么能从应用程序组件html导航到另一个组件在2000毫秒毫秒没有点击任何。我尝试使用router.navigate方法,但它不工作。它停留在应用程序组件html只要。 应用component.ts角2自动路由几秒后
import { Component ,OnInit} from '@angular/core';
import {RouterModule,Router} from '@angular/router';
import { SecondComponent } from './second/second.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private router: Router){}
ngAfterViewInit() {
setTimeout(() => {
this.router.navigate(["/second"]);
}, 2000);
}
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { SecondComponent } from './second/second.component';
import {RouterModule,Router,Routes} from '@angular/router'
const appRoutes: Routes = [
{ path: 'second', component: SecondComponent },
];
@NgModule({
declarations: [
AppComponent,
SecondComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
],
bootstrap: [AppComponent]
})
export class AppModule {
}
应用component.html:
<div class="my-container">
<h3 id ="h31">Digit</h3>
</div>
<router-outlet></router-outlet>
secondpage component.html:我想2秒
<div class="my-container">
<h3>Insurance made simple</h3>
<button type="button" class="btn btn-warning">Lets get started</button>
</div>
请提供你已经尝试过了 – GurV
应用component.ts –
是目标成分相同(添加PARAMS或东西)或者不同的代码? – GurV