Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ember服务在注入组件并在计算属性中使用后未定义_Javascript_Ember.js - Fatal编程技术网

Javascript Ember服务在注入组件并在计算属性中使用后未定义

Javascript Ember服务在注入组件并在计算属性中使用后未定义,javascript,ember.js,Javascript,Ember.js,我已经做了一个相对简单的Ember服务,并试图通过and将其注入到一个组件中。但是,在注入服务并通过计算属性调用其中一个函数后,我得到了错误: TypeError: undefined is not an object (evaluating 'this.get('businessValidator').validate') 我已经尝试使用调试器注销这个.get('businessValidator'),它返回未定义的,就像使用console.log一样。我尝试了按名称(如下所示)和隐式(不带

我已经做了一个相对简单的Ember服务,并试图通过and将其注入到一个组件中。但是,在注入服务并通过计算属性调用其中一个函数后,我得到了错误:

TypeError: undefined is not an object (evaluating 'this.get('businessValidator').validate')
我已经尝试使用调试器注销这个.get('businessValidator'),它返回未定义的,就像使用console.log一样。我尝试了按名称(如下所示)和隐式(不带名称)初始化该组件,因为它应该根据Ember和docs工作。传递给组件的模型是businessValidation服务需要验证的业务。我已经对服务本身进行了大量的单元测试

/components/production checker.js:

import Ember from 'ember';

export default Ember.Component.extend({
  businessValidator: Ember.inject.service('business-validator'),
  doesBusinessValidate: Ember.computed('model', function() {
     if (this.get('businessValidator').validate(this.get('model'))) {
      return "Ready for business!";
     } else {
       return "Production is halted!";
     }
  })
});
import Ember from 'ember';

export default Ember.Service.extend({
  init() {
    this._super(...arguments);
  },
  validate(business) {
    let validMarkets = this.validateTargetMarkets(business);
    let validChosenProduct = this.validateChosenProduct(business);
    let validSuppliers = this.validateRequiredSuppliers(business);
    let validPrice = this.validatePrice(business);
    let validWorkers = this.validateWorkers(business);
    let validLocations = this.validateLocations(business);
    if (validMarkets && validChosenProduct && validSuppliers && validPrice && validWorkers && validLocations) {
      return true;
    } else {
      return false;
    }
  },
  validateTargetMarkets(business) {
    if (business.get("targetMarkets") && business.get('targetMarkets').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateChosenProduct(business) {
    if (business.get("chosenProduct")) {
      return true;
    } else {
      return false;
    }
  },
  validateRequiredSuppliers(business) {
    let result = false;
    if (business.get("chosenProduct") && business.get("chosenProduct").get("requiredResources") && business.get("chosenProduct").get("requiredResources").length > 0) {
      result = business.get("chosenProduct").get("requiredResources").every(function(item) {
        if (item.get('chosenSupplier')) {
          return true;
        } else {
          return false;
        }
      });
    }

    if (result) {
      return true;
    } else {
      return false;
    }
  },
  validatePrice(business) {
    if (business.get('chosenProduct') && business.get('chosenProduct').get('price') && business.get('chosenProduct').get('price') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateWorkers(business) {
    if (business.get('workers') && business.get('workers').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateLocations(business) {
    if (business.get('locations') && business.get('locations').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  }

});
services/business validator.js:

import Ember from 'ember';

export default Ember.Component.extend({
  businessValidator: Ember.inject.service('business-validator'),
  doesBusinessValidate: Ember.computed('model', function() {
     if (this.get('businessValidator').validate(this.get('model'))) {
      return "Ready for business!";
     } else {
       return "Production is halted!";
     }
  })
});
import Ember from 'ember';

export default Ember.Service.extend({
  init() {
    this._super(...arguments);
  },
  validate(business) {
    let validMarkets = this.validateTargetMarkets(business);
    let validChosenProduct = this.validateChosenProduct(business);
    let validSuppliers = this.validateRequiredSuppliers(business);
    let validPrice = this.validatePrice(business);
    let validWorkers = this.validateWorkers(business);
    let validLocations = this.validateLocations(business);
    if (validMarkets && validChosenProduct && validSuppliers && validPrice && validWorkers && validLocations) {
      return true;
    } else {
      return false;
    }
  },
  validateTargetMarkets(business) {
    if (business.get("targetMarkets") && business.get('targetMarkets').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateChosenProduct(business) {
    if (business.get("chosenProduct")) {
      return true;
    } else {
      return false;
    }
  },
  validateRequiredSuppliers(business) {
    let result = false;
    if (business.get("chosenProduct") && business.get("chosenProduct").get("requiredResources") && business.get("chosenProduct").get("requiredResources").length > 0) {
      result = business.get("chosenProduct").get("requiredResources").every(function(item) {
        if (item.get('chosenSupplier')) {
          return true;
        } else {
          return false;
        }
      });
    }

    if (result) {
      return true;
    } else {
      return false;
    }
  },
  validatePrice(business) {
    if (business.get('chosenProduct') && business.get('chosenProduct').get('price') && business.get('chosenProduct').get('price') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateWorkers(business) {
    if (business.get('workers') && business.get('workers').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  },
  validateLocations(business) {
    if (business.get('locations') && business.get('locations').get('length') > 0) {
      return true;
    } else {
      return false;
    }
  }

});
余烬版本:

Ember      : 2.5.1
Ember Data : 2.5.2
jQuery     : 2.2.3

谢谢你的帮助

实际上,您的代码应该可以工作。结帐

因此,您的问题与您发布的代码无关

也许你没有使用最新的灰烬版本


另外,如果您向我们展示您的代码或一个不起作用的小玩意儿,我们可以告诉您更多,但对于这个问题,一切都在起作用。

这里可能不是问题所在,但我在开发插件时花了一些时间。我的服务总是未定义的,即使在我的comp中使用this.get('service_name')。这是访问它们的方法,因为属性是lazyloaded()

我发现该服务也必须存在于app文件夹中,而不仅仅存在于addon/service文件夹中,然后指向addons服务


使用
ember generate service service\u name
创建所有需要的文件…

不要使用
this.get('validateTargetMarkets')(business)
调用方法,只需说
this.validateTargetMarkets(business)
。虽然这可能不是你的核心问题。而且,如果(b)返回true,那么说
返回b
总比说
好;否则返回false
。下面的工作可以吗:
this.get('businessValidator.validate')(this.get('model');
?您使用的是什么版本的ember?在上面的帖子中添加了ember版本。关于调用方法,我很乐意在不使用.get(函数)的情况下调用它们,但我的印象是,如果服务未初始化,我应该这样做。因为这是一个内部函数调用,所以我不必担心这一点?很有趣。这应该是一个注释。使用Ember twiddle表明这种模式应该有效。我启动了我的笔记本电脑和Ember服务器,开始测试东西和TADA——我从昨晚突然工作正常。可能是因为在创建服务或组件后没有重新启动Ember服务器,但今天早上的测试工作正常。我想知道是否出于某种原因,我的文件系统在创建服务文件后没有确认服务文件,但它看起来仍然是Ghos对于未来的人:用Ember Twiddle验证一个测试用例!有同样的问题。重新启动“Ember服务器”修复了它-没有任何额外的操作。