1
我想要在加載/呈現頁面後使用實際值異步更新佔位符(currentUsername)。正在更新vue js值
我嘗試過一個示例項目(https://github.com/jackfranklin/vue2-demo-proj),但我無法將其與我的設置相關聯。使用vuejs 2.0。 我可以看到事件已觸發,通話已完成並返回了某些內容,但我的Chrome控制檯沒有錯誤,但在用戶界面中沒有更新。這是爲什麼?
1)HTML文件:
<script src="assets/js/data.js"></script>
2)data.js:
"use strict";
import * as DataXXX from './data.vue';
Vue.component("dataxxx", DataXXX);
var vm = new Vue({
el: "#app",
});
3)data.vue
<template>
<div>
<p v-if="currentUsername == null">
Null username
</p>
<p v-else>
Username: {{ currentUsername }}:
</p>
</div>
</template>
<script>
"use strict";
export default
{
created()
{
awr.EventsBus.$on("requestLoadData", this.loadData);
awr.EventsBus.$emit("requestLoadData", this.loadData);
},
destroyed()
{
awr.EventsBus.$off("requestLoadData", this.loadData);
},
methods:
{
loadData(name)
{
alert("got event");
this.currentUsername = name;
this.fetchGithubData(name);
},
fetchGithubData(name)
{
const url = "http://127.0.0.1/getUsername.php";
alert("requesting url: " + url);
fetch(url).then(response => response.text()).then(data => {
this.currentUsername = data;
console.log("got response: " + data);
});
}
},
data() {
return {
currentUsername: null
}
}
}
</script>