Javascript 如何在视图渲染之前获取ajax和分配数据

Javascript 如何在视图渲染之前获取ajax和分配数据,javascript,html,vue.js,Javascript,Html,Vue.js,我尝试将ajax.get放入created函数中,但仍然无法获得ap_name值 vue.js created: function () { ajax.get('/envs').then(function (res) { this.apName = res.AP_NAME; console.log(this.apName); }); }, html 您需要使用箭头函数,以便此不会被传递函数的上下文所掩盖: 已创建

我尝试将ajax.get放入created函数中,但仍然无法获得ap_name值

vue.js

created: function () {
        ajax.get('/envs').then(function (res) {
            this.apName = res.AP_NAME;
            console.log(this.apName);
        });
 },
html


您需要使用箭头函数,以便
不会被传递函数的上下文所掩盖:

已创建:函数(){
get('/envs')。然后((res)=>{
this.apName=res.AP\u NAME;
console.log(this.apName);
});
},
您可能需要将此行更改为:

this.apName=res.data.AP\u NAME;
 <a :href="`${apName}/home`"</a>