You can't write vue code in the timer?

A create account page, and if you create a result, I will ask him to jump to the list page.
I write the page jump code in the timer, but I can"t execute it.
if the timer is removed, it will be normal
what is the reason

axios.post("/campaign/add",campaignEntity,{
    headers: {
        Authorization : getCookie("token"),
    },
})
.then(res=>{
    if(res.data.status == 1){
        this.$message({
            message: "",
            type: "success",
            duration:1800
        });
        setTimeout(function()
        {
            alert(1111);  //
            this.$router.push({path:"/campaign"}); //push
        },1850)
    }
})

this this this !

setTimeout(()=>{
    alert(1111);  //
    this.$router.push({path:"/campaign"}); //push
},1850)

this direction has changed


try changing the code:

setTimeout(() => {
    alert(1111);  //
    this.$router.push({path:"/campaign"});
}, 1850)

setTimeout does not pass an arrow function, and you also write that when the this, function is executed, this points to window,window without the $router attribute.

solution: either switch to the arrow function and let this point to the this; in the outer domain, or use the self variable inside the outer var self=this; function to call self.$router.


scope, call with that

in an external declaration of let that = this;.
Menu