1. 需求
进入页面默认选中其中一个按钮,当跳转到其他页面返回的时候默认选中之前选中的按钮
发现elementui的autofocus属性不生效,原因是上面存在其他组件,如下图,存在el-badge:
2. 解决方法
自定义指令
Vue.directive('focus',{
inserted: function(el,binding){
if(binding.value){
el.focus();
}
}
})
使用:当v-focus里为true时聚焦
<el-badge v-for="(item,index) in archiveBlog" :key="index" :value="item.count" :max="99" class="item">
<el-button v-focus="(item.year==year)&&(item.month==month)" size="small">{{item.year}}年{{item.month}}月</el-button>
</el-badge>