在特定条件下如何执行部分代码

时间:2018-08-13 09:28:42

标签: javascript jquery

因此,我添加

var registry = require('uiRegistry');
var component = registry.get('checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset.street.0'); 

component.value(window.checkoutConfig.customer_address['street']);

但是component为空。 我想出了一个解决方案:

var timerId = setInterval(function setAddress() {
                var registry = require('uiRegistry');
                if(registry.get('checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset.street.0')!=null) {
                    var component = registry.get('checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset.street.0');
                    component.value(window.checkoutConfig.customer_address['street']);
                    clearInterval(timerId);
                }
            },2000);

但是我认为这是一个不好的解决方案。可能jQuery有类似ready的事件吗?例如,registry.get('checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset.street.0')!=null何时开始执行? 预先感谢

2 个答案:

答案 0 :(得分:0)

尝试一下。

require('uiRegistry', function(registry){
       var component = registry.get('checkout.steps.shipping- 
       step.shippingAddress.shipping-address-fieldset.street.0'); 
       if(component){
         component.value(window.checkoutConfig.customer_address['street']);
       }
});

答案 1 :(得分:0)

您可以尝试这样的方法->

registry.get('checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset.street.0', (value) => { }); 

可能是这项工作吗?您可以利用价值来完成工作