如何获取vue中所有data名称及数据

数据绑定后,需要循环获取data名及数据,一个一个写麻烦。直接在vue的methods中这样:

methods: {
        general_submit: function () {
          var b = '';
          for (var w in this._data) {
            b = b + w + '=' + this._data[w] + '\n';  // that's it, w是个string
          }
          alert(b);
        }
      }
axios 获取数据

https://www.jianshu.com/p/4ee31fdb78b6

通过cdn引用
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

get

// created:vue生命周期中的钩子函数,在这个时间点,data中的数据已经注入到响应式系统中
created(){
    axios.get('api/getData.php',{       // 还可以直接把参数拼接在url后边
        params:{
            title:'眼镜'
        }
    }).then(function(res){
        this.goodsList = res.data;	// 加上then,res.data就是数据
    }).catch(function (error) {
        console.log(error);
    });
}

post

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

// 注意: 如果发送请求时,发现传递的参数是对象,那么可用如下方式传参数
// var params = new URLSearchParams();
// params.append('title', '眼镜');
// params.append('id',1);
// axios.post('/user', params)
//      .then(function(res){})
//      .catch(function(error){});
在网页中插入网络代码
var importJs=document.createElement('script')//在页面新建一个script标签
    importJs.setAttribute("type","text/javascript")//给script标签增加type属性
    importJs.setAttribute("src", 'https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js') //给script标签增加src属性, url地址为cdn公共库里的
    document.getElementsByTagName("head")[0].appendChild(importJs)
如何用axios进行传参
https://www.cnblogs.com/zhaojunhao/p/9622004.html
Logo

前往低代码交流专区

更多推荐