首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在更改Ionic视图后,流星订阅不会停止。

在更改Ionic视图后,流星订阅不会停止。
EN

Stack Overflow用户
提问于 2016-05-20 14:30:19
回答 1查看 82关注 0票数 0

我将角流星离子型结合使用。

流星版本:1.3.2.4,角形版本:1.5.5,离子版本:1.2.4

我也在我的流星/角应用程序中使用ES2015。

有一种观点是:

代码语言:javascript
复制
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';

import { Meteor } from 'meteor/meteor';
import { SomeCollection } from 'path/to/collection';

import './view1.html';

class View1 {
  constructor($scope, $reactive, $state) {
    this.$state = $state;

    this.$scope = $scope;

    $reactive(this).attach($scope);

    this.subscribe('my.subscription');

    this.helpers({
      list: () => {
        return SomeCollection.find({});
      }
    });
  }
}

export default angular.module('view1', [
  angularMeteor,
  uiRouter,
  View1
]).config(config);

function config($stateProvider) {
  'ngInject';

  $stateProvider.state('app.view1', {
    url: '/view1',
    template: '<view-1></view-1>',
    views: {
      'appContent': {
        controller: View1,
        controllerAs: 'view1',
        templateUrl: 'path/to/template.html'
      }
    }
  });
}

而另一个视图我有类似的设置类型,但我订阅的是不同的出版物。

类似于:

代码语言:javascript
复制
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';

import { Meteor } from 'meteor/meteor';
import { SomeOtherCollection } from 'path/to/other/collection';

import './view2.html';

class View2 {
  constructor($scope, $reactive, $state) {
    this.$state = $state;

    this.$scope = $scope;

    $reactive(this).attach($scope);

    this.subscribe('my.other.subscription');

    this.helpers({
      list: () => {
        return SomeOtherCollection.find({});
      }
    });
  }
}

export default angular.module('view2', [
  angularMeteor,
  uiRouter,
  View2
]).config(config);

function config($stateProvider) {
  'ngInject';

  $stateProvider.state('app.view2', {
    url: '/view2',
    template: '<view-2></view-2>',
    views: {
      'appContent': {
        controller: View2,
        controllerAs: 'view2',
        templateUrl: 'path/to/other/template.html'
      }
    }
  });
}

当我在Ionic视图之间切换时,我注意到我仍然在从以前视图的订阅中轮询。根据文档,如果我使用$scope,应该自动调用stop()函数。

但是,我可以看到使用蒙古族时,我的订阅并没有停止切换视图。

目前,我已经能够使用快速修复,但它很难看,并且强制使用我认为不应该使用的变通方法填充代码,即:

代码语言:javascript
复制
$scope.$on('$ionicView.enter', () => {
  this.subscriptions = [];
  this.subscriptions.push(this.subscribe('some.collection'));
  this.subscriptions.push(this.subscribe('some.other.collection'));
});

$scope.$on('$ionicView.leave', () => {
  for (var sub in this.subscriptions) {
    this.subscriptions[sub].stop();
  }
});

我做错了什么?当在Ionic视图之间切换时,如何正确地停止订阅?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-20 14:52:51

默认情况下,离子缓存控制器/视图,因此我的作用域没有被破坏,因此在视图之间切换时没有调用stop()函数。

要解决这个问题,我只需在cache: false上设置$stateProvider,这就解决了我的问题。

但是,我确实喜欢我的视图被缓存,所以我想我会想出一种很好的方法来停止订阅,当在视图之间切换但是保持cache: true.任何优雅的想法都是欢迎的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37349402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档