调用unsubscribe时,Observable为null

时间:2016-11-08 17:50:19

标签: angular typescript rxjs rxjs5

我试图返回一个将按如下方式返回数据的observable。

getRootGroupNodes(): Observable<Group[]> {


    return Observable.create(function(observer) {

        var groups = [
            { groupName: "Group1" },
            { groupName: "Group2" }
        ]
        observer.next(groups);

        observer.complete();

    });
}

当我尝试消费时

   this._loadGroupsSubscription = this._apiGroupService.getRootGroupNodes()
        .retry(3)
        .subscribe(
        groups => {
            // do somethign with groups
        },
        err => { this._log.logMessage("failed to retrieve groups"); },
        () => {

            this._loadGroupsSubscription.unsubscribe();
        }
        );

我的this._loadGroupsSubscriptionnull。因此,尝试在undefined上调用unsubscribe时会抛出异常。我有什么想法,我做错了什么?这应该是基本的..

1 个答案:

答案 0 :(得分:2)

确实,变量未在回调中分配。我认为有两种方法可以解决这个问题:

  1. 使用 public class UsuarioController : Controller { private SiteDbContext db = new SiteDbContext(); // GET: Usuario public ActionResult Index() { return View(Mapper.Map<List<Usuario>, List<UsuarioIndexViewModel>>(db.Usuarios.ToList())); } // GET: Usuario/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Usuario usuario = db.Usuarios.Find(id); if (usuario == null) { return HttpNotFound(); } return View(Mapper.Map<Usuario, UsuarioIndexViewModel>(usuario)); } // GET: Usuario/Create public ActionResult Create() { return View(); } // POST: Usuario/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Nome,Email,Senha,Observacoes")] UsuarioViewModel viewModel) { if (ModelState.IsValid) { Usuario usuario = Mapper.Map<UsuarioViewModel, Usuario>(viewModel); db.Usuarios.Add(usuario); db.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel); } // GET: Usuario/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Usuario usuario = db.Usuarios.Find(id); if (usuario == null) { return HttpNotFound(); } return View(Mapper.Map<Usuario, UsuarioViewModel>(usuario)); } // POST: Usuario/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Nome,Email,Senha,Observacoes")] UsuarioViewModel viewModel) { if (ModelState.IsValid) { Usuario usuario = Mapper.Map<UsuarioViewModel, Usuario>(viewModel); db.Entry(usuario).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel); } // GET: Usuario/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Usuario usuario = db.Usuarios.Find(id); if (usuario == null) { return HttpNotFound(); } return View(Mapper.Map<Usuario, UsuarioIndexViewModel>(usuario)); } // POST: Usuario/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Usuario usuario = db.Usuarios.Find(id); db.Usuarios.Remove(usuario); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } 包装取消订阅:

    setTimeout()
  2. 添加setTimeout(() => _loadGroupsSubscription.unsubscribe()); 运算符,这基本上是相同的技巧:

    .delay(0)
  3. 查看现场演示:https://jsbin.com/sunicoq/2/edit?js,console

    查看一个稍微类似的问题并解释其工作原理:Other operator in calculation chain than combineLatest to avoid redundant calculations