鍍金池/ 問答/Linux  HTML/ 關(guān)于js原生事件使用vue的function

關(guān)于js原生事件使用vue的function

我想在點(diǎn)擊window的時(shí)候修改vue中data的值和使用methods的function。具體需要怎么做?vue-cli搭建的

回答
編輯回答
朕略萌

在組件的生命周期函數(shù)里面進(jìn)行綁定就行了唄。

export default {
    data() {
        return {
            value: 0,
        };
    },
    methods: {
        change() {
            this.value++;
        },
    },
    mounted() {
        window.addEventListener('click', () => this.change());
    },
}
2017年5月18日 21:08
編輯回答
笨尐豬
data(){
    return{
        someData: 'aaa'
    }
},
created(){
    let _self = this;
    window.addEventListener('click',function(){
        _self.someData = 'bbb'
        _self.someMethod()
    })
},
methods: {
    someMethod(){
        alert(1)
    }
}
2017年11月12日 10:18