js-动态方法的实现

65 阅读1分钟

适用于类似switch场景,事件分发的处理方式

    
   <view @click="func(index)" v-for="(item,index) in arr"">
	{{'执行f' + (index+1) + '的方法 '}}
   </view>
    
    arr:['f1','f2','f3']

    func(index){
            // 核心
            this[this.arr[index]]()
    },
    f1(){
            console.log('f1')
    },
    f2(){
            console.log('f2')
    },
    f3(){
            console.log('f3')
    }