[improvement] Sidebar: use relation mixin

This commit is contained in:
陈嘉涵
2019-05-06 20:37:09 +08:00
parent 8de7a287f6
commit 9c6c10fdcd
3 changed files with 13 additions and 33 deletions

View File

@@ -1,43 +1,28 @@
import { use } from '../utils';
import Info from '../info';
import { ChildrenMixin } from '../mixins/relation';
const [sfc, bem] = use('sidebar-item');
export default sfc({
mixins: [ChildrenMixin('vanSidebar')],
props: {
url: String,
info: [String, Number],
title: String
},
inject: ['vanSidebar'],
created() {
this.parent.items.push(this);
},
beforeDestroy() {
this.parent.items = this.parent.items.filter(item => item !== this);
},
computed: {
parent() {
if (process.env.NODE_ENV !== 'production' && !this.vanSidebar) {
console.error('[Vant] SidebarItem needs to be child of Sidebar');
}
return this.vanSidebar;
},
select() {
return this.parent.items.indexOf(this) === +this.parent.activeKey;
return this.index === +this.parent.activeKey;
}
},
methods: {
onClick() {
const index = this.parent.items.indexOf(this);
this.$emit('click', index);
this.parent.$emit('change', index);
this.$emit('click', this.index);
this.parent.$emit('change', this.index);
}
},