
[英]function call within a vue js v-for directive makes the loop run into an infinite loop and I don't know why
[英]I don't understand why a div tag would stop being visible once I try to bind it to a v-for directive in my code
问题在于带有 class 日志的 div 标签,我正在尝试使用从 api 响应中获得的数据填充文本。 当我尝试包含 v-for 指令时,整个 div 将从浏览器中消失,并且控制台没有抛出任何错误。
<template>
<div>
<div class="log" v-for="info in infos" v-bind:key="info" v-text="info.login">
Username
</div>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
name: '',
infos: null,
}
},
methods: {
hello() {
let user = document.querySelector(".search").value;
let fullname = user.split(" ").join("");
let msg = "No Username Found";
const axios = require("axios");
axios.get("https://api.github.com/users/" + fullname)
.then(response => (this.infos = response.data))
.catch(error => alert(error + " " + msg))
},
reset() {
this.name = "";
}
}
};
</script>
infos 是 null 所以 div 不会显示。 下面我添加了一个创建的 function,我在其中调用 hello()。这将填充信息并显示 div。
<script>
export default {
name: "HelloWorld",
data() {
return {
name: '',
infos: null,
}
},
created() {
hello();
},
methods: {
hello() {
let user = document.querySelector(".search").value;
let fullname = user.split(" ").join("");
let msg = "No Username Found";
const axios = require("axios");
axios.get("https://api.github.com/users/" + fullname)
.then(response => (this.infos = response.data))
.catch(error => alert(error + " " + msg))
},
reset() {
this.name = "";
}
}
};
</script>
尝试使用计算方法
<template>
<div>
<div class="log" v-for="info in infosComputed" v-bind:key="info" v-text="info.login">
Username
</div>
</div>
</template>
...
computed {
infosComputed: function() {
return this.infos;
}
}
数据不是反应性的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.