如何在Ionic V4中读取本地JSON文件?

时间:2018-08-08 03:55:43

标签: ionic3 ionic4

this.http.get(‘assets/data/subtitles.json’).map(res => res.json()).subscribe(data => {
this.subtitles = data;
});

这是我使用过的ionic v3 localJSON读取代码。

但是离子型V4不支持离子型V3

2 个答案:

答案 0 :(得分:0)

您可以改用fetch

fetch('assets/data/subtitles.json').then(async res => {
  this.subtitles = await res.json();
});

答案 1 :(得分:0)

或者,您也可以将json读为导入

/* AJAX function to create and save fee to the order ** TEST */
add_action('wp_ajax_create_fee_object', 'create_fee_object');
add_action('wp_ajax_nopriv_create_fee_object', 'please_login');
function create_fee_object() {

  if ( !wp_verify_nonce( $_REQUEST['nonce'], 'add_custom_fee_nonce' ) ) {
    exit("Unsecured access detected!");
  }

  $add_fee_toggle = $_REQUEST['add_fee_toggle'];
  $fee_name = $_REQUEST['add_fee_name'];
  $fee_percentage = $_REQUEST['add_fee_percentage'] / 100 ;

  $order_id = $_REQUEST['order_id'];
  $order = wc_get_order( $order_id );

  if( $add_fee_toggle == '1' ) :
    $subtotal = $order->get_subtotal();
    $fee_amount = $subtotal * $fee_percentage;
    // Set up the fee object
    $fee = array(
      'name' => $fee_name,
      'amount' => $fee_amount,
      'taxable' => true,
      'tax_class' => ''
    );

    // Add fee to the order
    $item_id = wc_add_order_item( $order_id, array(
      'order_item_name' => $fee['name'],
      'order_item_type' => 'fee'
    ) );
    if ( ! $item_id ) {
      return false;
    }
    if ( $fee['taxable'] ) {
      wc_add_order_item_meta( $item_id, '_tax_class', $fee['tax_class'] );
    } else {
      wc_add_order_item_meta( $item_id, '_tax_class', '0');
    }

    wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $fee['amount'] ) );
    wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $fee['tax'] ));

    // Save tax data - Since 2.2
    $tax_data = array_map( 'wc_format_decimal', $fee['tax_data'] );
    wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $tax_data ) );

    do_action( 'woocommerce_order_add_fee', $order_id, $item_id, $fee );

    // Update the fee toggle back to default value ("No")
    $update_fee = update_post_meta( $order_id, 'add_fee_toggle', '0' );

    if ( $update_fee === false ) {
      $result['type'] = "error";
    } else {
      $result['type'] = "success";
    }

    // Check if action was fired via Ajax call. If yes, JS code will be triggered, else the user is redirected to the post page
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
      $result = json_encode($result);
      echo $result;
    } else {
      header("Location: ".$_SERVER["HTTP_REFERER"]);
    }

  endif;

  die();
}

function please_login() {
  alert("You must log in to do this.");
  die();
}

Typescript可能会抱怨导入与您的类型不匹配,因此将其强制转换为import * as subtitleJson from 'assets/data/subtitles.json'; 即可,

any