0

我正在玩 angular2 和流星(我都是新手),如果我能在集合中找到某个文档,我想改变路线。我在Angular 2 Meteor 教程中读到 MeteorComponent 类有方法 subscribe 和 autorun 所以我正在尝试使用这些方法来完成工作(不确定这是否是最好的方法 - 我没有删除 autopublish )。但现在它不起作用。我的组件是:

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
  invitation: Mongo.Cursor<Object>

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

  getInvitation() {
    this.subscribe('invitations', () => {
      this.invitation = Invitations.find({$or: [
        {$and: [
          {"sender._id": this.currentPlayer._id},
          {status: 1}
        ]},
        {$and: [
          {"recipient._id": this.currentPlayer._id},
          {status: 1}
        ]}
      ]})
      this.autorun(() => {
        console.log('autorun')

        if(this.invitation){
          console.log('game started')
          this._router.navigate(['Game'])
        }
      })
    })
  }

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

  ngOnInit() {
    this.getPlayer()
    this.getInvitation()
  }
}

在我的幻想中,在 ngOnInit 中调用的 getInvitation() 应该订阅“邀请”集合,并且 autorun() 应该检查是否找到了文档,如果找到了应该改变路由。但我没有收到错误,路线也没有改变。哪一个应该是响应性地更改路由以响应集合更改的正确方法?

4

1 回答 1

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'])
      }
    })
  }
}
于 2016-01-19T00:08:56.373 回答