ant-design-vue/components/_util/BaseMixin2.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-10-28 07:31:45 +00:00
import { nextTick } from 'vue';
2020-08-12 08:30:13 +00:00
import { getOptionProps } from './props-util';
export default {
methods: {
2020-10-02 07:44:10 +00:00
setState(state = {}, callback) {
2020-08-12 08:30:13 +00:00
let newState = typeof state === 'function' ? state(this, this.$props) : state;
if (this.getDerivedStateFromProps) {
const s = this.getDerivedStateFromProps(getOptionProps(this), {
...this,
...newState,
});
if (s === null) {
return;
} else {
newState = { ...newState, ...(s || {}) };
}
}
Object.assign(this, newState);
if (this._.isMounted) {
this.$forceUpdate();
}
2020-10-28 07:31:45 +00:00
nextTick(() => {
2020-08-12 08:30:13 +00:00
callback && callback();
});
},
__emit() {
// 直接调用事件底层组件不需要vueTool记录events
const args = [].slice.call(arguments, 0);
let eventName = args[0];
eventName = `on${eventName[0].toUpperCase()}${eventName.substring(1)}`;
const event = this.$props[eventName] || this.$attrs[eventName];
if (args.length && event) {
if (Array.isArray(event)) {
for (let i = 0, l = event.length; i < l; i++) {
event[i](...args.slice(1));
}
} else {
event(...args.slice(1));
}
}
},
},
};