<script>
window.onload = function () {
new Vue({
el:'#box',
data:{
message:'',
text:[],
file:[],
txt:''
},
methods:{
get:function () {
var ob = this;
var test;
if(window.XMLHttpRequest){
test = new XMLHttpRequest();
}else if(window.ActiveXObject){
test = new window.ActiveXObject();
}else{
alert("请升级至最新版本的浏览器");
}
if(test !=null){
test.open("GET","https://api.bootcdn.cn/libraries.json",true);
test.send(null);
test.onreadystatechange=function(){
if(test.readyState==4&&test.status==200){
var obj = JSON.parse(test.responseText);
for (var i = 0; i<obj.length; i++){
ob.text.push(obj[i]);
console.debug("arr",ob.text)
}
}
};
}
},
push:function () {
var ob = this;
console.debug("arr",ob.text);
for(var i = 0; i<this.text.length; i++) {
console.debug("ok");
}
}
}
})
}
</script>
这里text[]根本没有值,请问我要怎么改呢?谢谢
我运行了一下是有值的。你是不是调用自己定义的get方法然后立刻调用push方法。如果是这样的话,ajax请求还没有完成,所以text没有值。