Commit 1215b5a6 authored by liziwl's avatar liziwl
Browse files

简化函数

parent 31bfd6a8
Loading
Loading
Loading
Loading
+3 −26
Original line number Diff line number Diff line
@@ -2,30 +2,7 @@
  <slot :data="data"></slot>
</template>
<script>
export function fetchJSONData(url) {
  return new Promise(function (resolve, reject) {
    let xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.onload = function () {
      if (this.status == 200 && this.status < 300) {
        let data = JSON.parse(xhr.response);
        resolve(data);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText,
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText,
      });
    };
    xhr.send();
  });
}
import axios from "axios";
export default {
  data() {
    return {
@@ -38,8 +15,8 @@ export default {
  watch: {
    path(val) {
      if (val)
        return fetchJSONData(val).then((data) => {
          this.data = data;
        return axios.get(val).then((response) => {
          this.data = response.data;
        });
    },
  },