鍍金池/ 問(wèn)答/HTML/ vue export default 外部調(diào)用內(nèi)部methods

vue export default 外部調(diào)用內(nèi)部methods

<script>
    //在想這里調(diào)用 methods 里面的方法 因?yàn)檫@個(gè)不能寫(xiě)到里面
    $(document).on('refresh', '.pull-to-refresh-content', function (e) {
        this.getList();// 想在這里地調(diào)用里面的 getList  但是報(bào)錯(cuò)
        console.log('123');
    });
    
    export default {
        name: "friends",
        data() {
            return {
                articles: []
            }
        },
        methods: {
            getList: function () {
                
            }
        },
    }
</script>
回答
編輯回答
荒城

你可以把getlist卸載export default 外面 以一種純函數(shù)的方式去寫(xiě)

function getList (pagesize,pages){
  return this.$axios.get('xxx') .....
}

然后在methods中

methods: {
getListInExpDau(){
 getList(10,1) ....
}
2018年1月24日 01:46
編輯回答
怣痛

你可以這樣寫(xiě)

<script>
    //在想這里調(diào)用 methods 里面的方法 因?yàn)檫@個(gè)不能寫(xiě)到里面
    $(document).on('refresh', '.pull-to-refresh-content', function (e) {
        friends.methods.getList();// 想在這里地調(diào)用里面的 getList  但是報(bào)錯(cuò)
        console.log('123');
    });
    
    const friends = {
        name: "friends",
        data() {
            return {
                articles: []
            }
        },
        methods: {
            getList: function () {
                
            }
        },
    }
    export default friends;
</script>
2017年10月13日 13:00
編輯回答
逗婦乳

如果有多個(gè)組件實(shí)例,你要調(diào)哪個(gè)實(shí)例的method?
如果組件銷毀后,觸發(fā)事件,調(diào)用已銷毀實(shí)例的method會(huì)怎么樣你考慮過(guò)嗎?

請(qǐng)你老老實(shí)實(shí)在mounted里寫(xiě)這些代碼

2018年7月17日 01:12
編輯回答
離觴

這個(gè)可以寫(xiě)在里面把

export default {
        name: "friends",
        data() {
            return {
                articles: []
            }
        },
        mounted() {
            $(document).on('refresh', '.pull-to-refresh-content', () => {
                this.getList();
                console.log('123');
            });
        },
        destroyed() {
            $(document).off('refresh', '.pull-to-refresh-content');
        },
        methods: {
            getList: function () {
                
            }
        },
    }
2018年8月18日 07:00
編輯回答
撥弦

這種方法也許可以

<script>
    //在想這里調(diào)用 methods 里面的方法 因?yàn)檫@個(gè)不能寫(xiě)到里面
    $(document).on('refresh', '.pull-to-refresh-content', function (e) {
        vm && vm.getList();// 想在這里地調(diào)用里面的 getList  但是報(bào)錯(cuò)
        console.log('123');
    });
    
    var vm = null;
    
    export default {
        name: "friends",
        data() {
            return {
                articles: []
            }
        },
        created: function() {
            vm = this
        },
        methods: {
            getList: function () {
                
            }
        },
    }
</script>
2018年6月24日 04:05