rubie
rubie

Reputation: 67

How I get ajax and assign data before view render

I try put the ajax.get in created function but i still can't get the ap_name value

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>

where is the problem?

Upvotes: 1

Views: 63

Answers (1)

Dan
Dan

Reputation: 63059

You need to use an arrow function so that this isn't obscured by the context of the passed function:

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

And you may need to change this line to:

this.apName = res.data.AP_NAME;

Upvotes: 1

Related Questions