Home > Net >  The ajax request functions in a vue unable to get the vue variables
The ajax request functions in a vue unable to get the vue variables

Time:01-24

Vue the methods: the inside of the function if including the ajax request, need to use Vue variables of time, can't use this. Xx, this way, only to declare a global change,
in the following wayGiveprize () {
//get the select value
fha01=this. Hha01;

$. Ajax ({
Url:/Hpaward Editbj,
Type: "POST",
Data: jsdata,
DataType: "JSON",
ContentType: "application/json,"
Success: the function (res) {
If (res) flag) {
//dynamically create node
Var li=document. The createElement method (" li ");
li. InnerHTML=fha01;
Why,

CodePudding user response:

Don't c #, Javascript you thought this was fixed?
In JavaScript, this is not fixed, it will change with the change of the execution environment,
In the method, this says the method's object,
If used separately, this means the global object,
In function, this says global object,
In function, under the strict mode, this is undefined (undefined),
In the event, this said receiving element of events,

CodePudding user response:

Javascript is not an object-oriented language, it is at best "based on the object's" language, this is used to point to call a function object, or call a function statements use the bind (... ) method explicitly passed this parameter, in object-oriented languages, this method can point on to belong to the object instance, rather than an object-oriented language, js, for example, its this just a casually can modify the value of the parameter,

CodePudding user response:

To design a software, you can either design a clear object, or to determine the reference parameters through giveprize pass, don't abuse "global variables",

CodePudding user response:

Do you use jquery?

Try arrow function, if you find that this is not what you want this,
 
Success: (res)=& gt; {
.
}

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arrow_functions

Javascript is an amazing language...
  • Related