xugy0926 / getting-started-with-javascript

164 stars 292 forks source link

如何把axios调用过来的数组通过函数平分4份? #567

Open babysodme opened 7 years ago

babysodme commented 7 years ago

老师好:

我的诉求:把使用axios get过来的数组我想在平均分成四份赋值给四个数组。 我的写法:定义了fetchDatawords()方法,在方法里加里function(arr, len)将数组平分的方法(百度的);分割线那段; 我的问题:在浏览器看到get成功,不过在页面上渲染不出来。github地址:https://github.com/babysodme/homework_lesson7

<script>
 // 把收集的数据放在这里
      var app = new Vue({
        el: '#app',   // #app 和html中div#id的 app 对应
        data: {
          wordsList0: [],
          wordsList1: [],
          wordsList2: [],
          wordsList3: [],
          gitList: [], // 变量wordsList可以在html使用
        },
        methods: {
          fetchData: function () {
          axios.get('https://js.xinshengdaxue.com/api/v1/learnJS/course/1/catelog')
          .then(function (response) {
            app.gitList = response.data.catelog.gitTopicsList
          })
          .catch(function (error) {
            console.log(error);
          });
        },
        fetchDatawords: function () {
          var wordsList =[];
          axios.get('https://js.xinshengdaxue.com/api/v1/learnJS/course/1/words')
          .then(function (response) {
            wordsList = response.data.words
            (function(arr, len){
              var a_len = arr.length;
           var result = [];
           for (var i = 0; i < a_len; i += len) {
            result.push(arr.slice(i, i + len));
             }
           app.wordsList0 = result[0];
           app.wordsList1 = result[1];
           app.wordsList2 = result[2];
           app.wordsList3 = result[3];
            })(wordsList,4);
          })

          .catch(function (error) {
            console.log(error);
          });     
        },
          sendData: function () {
            var name = document.getElementById("name").value;
            var account = document.getElementById("account").value;
            var content = document.getElementById("content").value;
           axios.post('https://js.xinshengdaxue.com/api/v1/learnJS/sayToMe',
          {
            name: name,
            account: account,
            content: content,
          })
          .then(function (response) {
            console.log(response);
          })
          .catch(function (error) {
            console.log(error);
          });
        //TODO: 对老师说点什么呢?
          }
         }
      })
      app.fetchData();
      app.fetchDatawords();

      </script>
xugy0926 commented 7 years ago

image 这句后面漏了分号。导致代码出错。