加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在角度2中将一条路线导航到另一条路线

发布时间:2020-12-17 17:58:51 所属栏目:安全 来源:网络整理
导读:我制作了角度2的两个不同组件.我正在学习这个链接的路由 https://angular.io/docs/ts/latest/tutorial/toh-pt5.html 我制作了两个组件.在第一个组件中,我有一个按钮,我想移动到第二个组件 这是我的代码 https://plnkr.co/edit/GBOI9avZaPGaxaLQbtak 我定义这
我制作了角度2的两个不同组件.我正在学习这个链接的路由
https://angular.io/docs/ts/latest/tutorial/toh-pt5.html

我制作了两个组件.在第一个组件中,我有一个按钮,我想移动到第二个组件

这是我的代码
https://plnkr.co/edit/GBOI9avZaPGaxaLQbtak

我定义这样的路线

const routes =[
      {
        path: 'ft',component: First
      },{
        path: 'sd',component: Second
      }
    ]

@NgModule({
  imports: [ BrowserModule ],declarations: [ App ],bootstrap: [ App,First,Second]
})

我正在使用< router-outlet>

但我无法将一个组件移动到另一个组件

解决方法

你去:

@Component({
  selector: 'first',template: `
    <div>
      <button (click)="moveToSecond()">move to secon comp</button>
    </div>
  `,})
export class First {
  name:string;
  constructor(private router:Router) {
  }

  moveToSecond(){
    this.router.navigate(['/sd']);
  }
}

https://plnkr.co/edit/wIuaffLskQd8GJuqLlY6?p=preview

你有很多错误:D

无论如何,为了导航到另一条路线,您需要使用路由器

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读