角度2流星变换路线被动反应

时间:2016-01-16 18:06:17

标签: meteor angular angular2-meteor

我正在玩angular2和meteor(我是两个都是新手),如果我能在一个集合中找到某个文件,我想改变路线。我在Angular 2 Meteor tutorial中读到MeteorComponent类有方法订阅和自动运行所以我正在尝试使用这些方法来完成工作(不确定这是否是最好的方法 - 我没有删除自动发布)。 但是现在它没有用。 我的组件是:

b.C

在我的幻想中,在ngOnInit中调用的getInvitation()应该订阅 '邀请'集合和自动运行()应该检查文件是否被找到以及是否找到了该文件应该改变路线。 但是我没有得到路线改变的错误。 哪个应该是反应性更改收集路线的正确方法?

1 个答案:

答案 0 :(得分:0)

好吧,我正在使用this.autorun()这个方法。 这简单得多:

import {View, OnInit, Component} from 'angular2/core'
import {MeteorComponent} from 'angular2-meteor'
import {Navbar} from 'client/navbar/navbar'
import {FakePlayground} from 'client/intro/fake-playground/fake-playground'
import {PlayerList} from 'client/player-list/player-list'
import {PlayerService} from 'client/player/player-service'
import {Player} from 'client/player/player'
import {ProtectedDirective} from 'client/directives/protected-directive'
import {SentInvitation} from 'client/invitation/sent-invitation'
import {ReceivedInvitation} from 'client/invitation/received-invitation'
import {Invitations} from 'lib/invitations'
import {Router} from 'angular2/router'

@Component({
  selector: 'intro'
})
@View({
  templateUrl: 'client/intro/intro.html',
  directives: [
    Navbar,
    PlayerList,
    FakePlayground,
    ProtectedDirective,
    SentInvitation,
    ReceivedInvitation
  ]
})
export class Intro extends MeteorComponent implements OnInit {
  currentPlayer: Player

  constructor(
    public playerService: PlayerService,
    private _router:Router
  ) {
    super()
  }

  getPlayer() {
    this.currentPlayer = this.playerService.getCurrentPlayer()
  }

  ngOnInit() {
    this.getPlayer()
    this.autorun(() => {
      if (Invitations.findOne({
        $or: [
          {$and: [{"sender._id": this.currentPlayer._id},{status: 1}]},
          {$and: [{"recipient._id": this.currentPlayer._id},{status: 1}]}
        ]
      })) {
        this._router.navigate(['Game'])
      }
    })
  }
}