Vue中的this

 会用到vue2.0,vuex,mint ui中的无限加载,UI组建库 vux


  • 创建项目 sudo vue init airyland/vux2 this 

Project name this
? Project description A Vue.js project
? Author yestodorrow <power998@163.com>
? Vue build standalone
? Install vue-router? No
? Use ESLint to lint your code? No
? Setup unit tests No
? Setup e2e tests with Nightwatch? No复制代码

  • 下载安装依赖

npm i  
npm i mint-ui --save-dev复制代码
  • 引入vuex

    import Vue from 'vue'import Vuex from 'vuex'
    Vue.use(Vuex)
    const store= new Vuex.Store({    state:{        list:[]    },    actions:{        triggerActions(context,params){            console.log("triggered");
                if(params.cb){                params.cb("success")            }        }    },    mutations:{
        }})export default store
    复制代码

hellofromvux.vue中代码如下

<template>  <ul  v-infinite-scroll="loadMore"  infinite-scroll-disabled="loading"  infinite-scroll-distance="10">  <li v-for="(item,index) in list" :key="index">{{ item }}</li>  </ul>
</template>
<script>

export default {  data(){    return{      list:[1,2,3,5,6],      page:0    }  },  components: {  },    created:function(){    console.log(this);
  },  methods:{    loadMore() {      this.loading = true;      this.$store.dispatch("triggerActions",{        page:this.page,        a:function(msg){          if(msg=="success"){            console.log("success")            console.log(this);            this.page++;            this.loading = false;          }        }      })    }  }}</script>
<style>.vux-demo {  text-align: center;}.logo {  width: 100px;  height: 100px}</style>

复制代码

其中load more在页面创建就会调用;

我们发现

第一个this指向是VueComponent

第二个this指向是cb



也就是说this的指向发生了变化;

就是说在mint ui的分页组件中,如果要请求加载下一页,this已经不在指向Vue;

所以this.page++也就不会生效



解决办法:

let that=this;

that.page++



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值