是否有任何方法来指定一个属性在成员>=1.13中需要,以便如果它没有被传递,它将提供一个有用和明确的消息?
我想,一旦ember-validations最终得到更新,就会涵盖它。我只是想知道我是不是错过了什么。
发布于 2016-03-15 03:21:29
我知道我有点晚了,但是现在有了余烬-支柱-类型,它的API与React的API几乎相同。
以下是消费的一个例子:
import Ember from 'ember'
import PropTypeMixin, {PropTypes} from 'ember-prop-types'
export default Ember.Component.extend(PropTypeMixin, {
propTypes: {
foo: PropTypes.string,
bar: PropTypes.number.isRequired,
baz: PropTypes.oneOf([
PropTypes.bool,
PropTypes.string
])
},
getDefaultProps () {
return {
foo: 'This is going to be highly profitable'
}
}
})免责声明:我是这个项目的创造者。
发布于 2015-07-23 09:56:59
您可以使用Ember.assert作为属性的默认值,这样如果没有设置它,您就会得到一条错误消息。
Import Ember from 'ember';
const { computed, assert } = Ember;
myProperty: computed(function() {
return assert('My property cannot be empty');
})发布于 2016-03-08 20:13:15
正如上面提到的@Kilter,Ember.assert方法是最好的方法。
下面是一个小示例,虽然它不如React那样具有声明性,但效果也一样好。
didReceiveAttrs() {
this._super(...arguments);
assert('You must pass a model into the submit-button component', this.get('model'));
},https://stackoverflow.com/questions/31576025
复制相似问题