当类似与登陆等需求时,当登陆成功后才跳转,不成功则返回,则需要js路由跳转来处理
<div>
Home组件
<button (click)="goNews()">js跳转新闻</button>
</div>
import { Component, OnInit } from '@angular/core';
// 1 引入
import { Router } from '@angular/router'
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
// 2 申明
constructor(private router: Router) { }
ngOnInit() {
}
goNews() {
// alert('go')
// js跳转,他有第二个参数,1也就是跳转到/news/1
this.router.navigate(['/news','1'])
}
}