current position:Home>Remember Vue project code optimization

Remember Vue project code optimization

2022-01-27 02:25:10 Tmall elf 998

There is a requirement in the project , After switching the video playback source , You need to restore the previously set playback rate . It was found by experiment that , If after switching the playback source , Setting the playback rate immediately does not take effect , The settings will not take effect until the video is loaded , Therefore, you need to save a callback when switching the playback source , stay onPlay Set the playback rate when the event is triggered :

import bus from "EventBus";

export default {
    
	data() {
    
		return {
    
			rateChangeEvent: null;
		}
	},
	mounted() {
    
		bus.$on('onPlay', bus.$emit('handleSetRateAsync'));
	},
	methods: {
    
		handleReplaceSource() {
    
			// do something
			this.rateChangeEvent = () => console.log(' Set the playback rate ');
			bus.$on('handleSetRateAsync', () => {
    
				this.rateChangeEvent && this.rateChangeEvent();
				this.rateChangeEvent= null;
			})
		}
	}
}

The event needs to be triggered once , More elegant writing :

import bus from "EventBus";

export default {
    
	mounted() {
    
		bus.$on('onPlay', bus.$emit('handleSetRateAsync'));
	},
	methods: {
    
		handleReplaceSource() {
    
			// do something
			bus.$once(
				'handleSetRateAsync',
				() => console.log(' Set the playback rate ')
			);
		}
	}
}

Event triggers are written more elegantly :

import bus from "EventBus";

export default {
    
	methods: {
    
		handleReplaceSource() {
    
			// do something
			bus.$once(
				'onPlay',
				() => console.log(' Set the playback rate ')
			);
		}
	}
}

copyright notice
author[Tmall elf 998],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270225079909.html

Random recommended