1

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

Vue.js

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

html

 <a :href="`${apName}/home`"</a>

问题出在哪里?

4

1 回答 1

1

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

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

您可能需要将此行更改为:

this.apName = res.data.AP_NAME;
于 2020-11-18T03:28:45.833 回答