Javascript es2015导入语法

时间:2018-05-20 10:18:41

标签: javascript node.js webpack ecmascript-6 babel

我想要导入一部分包。我有这段代码:

import N, { Newsitem } from '../../newsitems/src/index'
class Home extends React.Component {
  render () {
    console.log('+++ N:', N )
    console.log('+++ Newsitem:', Newsitem)
    return (
      <div>
        <Row>
          <Col xs={12}>
            <h1>Home...</h1>
          </Col>
        </Row>
        <N.Newsitem />
      </div>)
  }
}

给我这个日志输出:

+++ N: { Newsitem: [Function: Newsitem],
  Newsitems: [Function: Newsitems] }
+++ Newsitem: undefined

如何仅导入包的一部分?我认为这种语法应该有用。

1 个答案:

答案 0 :(得分:1)

您必须显式导出而不是仅使用默认导出。 像

这样的东西
export {
  Newsitem,
  Newsitems
}

add_action( 'woocommerce_after_checkout_form', 'custom_checkout_jquery_script', 30 );
function custom_checkout_jquery_script() {
    if( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery(function($){
        // wc_checkout_params is required to continue
        if ( typeof wc_checkout_params === 'undefined' )
            return false;

        var a = '#billing_myfield5', b = a+' option:selected';
        $( 'form.checkout' ).on( 'change', a, function(){
            console.log('Chosen area: '+$(b).html()); // To be removed (testing)

            // Ajax: send the chosen customer location to php
            $.ajax({
                type: 'POST',
                url:.ajax_url,
                data: {
                    'action': 'set_customer_area',
                    'customer_area': $(b).html(),
                },
                success: function (response) {
                    $(document.body).trigger('update_checkout');
                    console.log('Response: '+response); // To be removed (testing)
                }
            });
        });
    });
    </script>
    <?php
}

// Wordpress Ajax: Saved the selected customer location to WC_Session
add_action( 'wp_ajax_nopriv_set_customer_area', 'set_customer_area_in_wc_sessions' );
add_action( 'wp_ajax_set_customer_area', 'set_customer_area_in_wc_sessions' );
function set_customer_area_in_wc_sessions() {
    if( ! isset($_POST['customer_area']) ) return;

    // Encoding the customer's location for Google API
    $customer_area_enc = rawurlencode( $_POST['customer_area'] );

    // Set the chosen customer location in WC_Sessions
    WC()->session->set('customer_area', rawurlencode($_POST['customer_area']) );

    // To be removed (testing: Send back the data to jQuery)
    echo json_encode( WC()->session->get('customer_area' ) );

    die(); // To avoid server error 500
}

// Add a fee based on the highest distance between customer and vendors
add_action( 'woocommerce_cart_calculate_fees', 'distance_shipping_fee', 30, 1 );
function distance_shipping_fee( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Get Url encoded customer area that is saved in WC_Session by ajax
    $customer_area = WC()->session->get('customer_area' );

    // Only when customer area has been selected
    if( empty($customer_area) )
        return;

    // Getting billing city of vendors
    foreach( $cart->get_cart() as $cart_item ){
        $vendor_id = get_post_field( 'post_author', $cart_item['product_id'] );
        $vendors[$vendor_id] = get_user_meta($vendor_id, 'billing_city', true);
    }
    $dist_array = array();

    // Loop through vendors locations
    foreach( $vendors as $vend_loc){

        // Setting Google API URL ##
        $gapi_key = MY_APY; // Set HERE your google api key
        $shippingurl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$vend_loc";
        $shippingurl .= "+dhaka+bangladesh&destinations=$customer_area+dhaka+bangladesh&key=$gapi_key";

        // Now fetching json response from googleapis:
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $shippingurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = json_decode(curl_exec($ch), true);

        // If google responds with a status of OK: Extract the distance text:
        if($response['status'] == "OK")
            $dist = $response['rows'][0]['elements'][0]['distance']['text'];

        $dist_array[] = preg_replace("/[^0-9\.]/", '', $dist);
    }

    // Get the bigger distance
    $distance = max ($dist_array);
    $distance = abs ($distance);

    $fee = $distance < 5 ? 10 : 20;

    if ( $distance && $fee > 0 )
        $cart->add_fee( "Delivery - Distance Rate", $fee , true);
}