LoginSignup
25
18

More than 5 years have passed since last update.

vue.jsのaxios内でthisを使う

Last updated at Posted at 2017-04-02

axios内

  • thisをbindすれば使える
app.js

var hoge = new Vue({

    el:'#hoge',
    data:{
        hoge:''
    },
    created:function(){
        axios.post('hogeapi', null)
            .then(function(res) {
              this.hoge = res.data;
           }.bind(this)).catch(function(err) {
              console.log(err);
           }.bind(this));
    }

});

forEach内

  • forEach内でも使える
app.js

var hoge = new Vue({

    el:'#hoge',
    data:{
        hoge:[],
        foo:''
    },
    methods:{
        hoge:function(){
            this.hoge.forEach(function(value,index,array){
                value = this.foo;
            }.bind(this))
        }
    }

})
25
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
18