Да, можно, вот демо:
Vue.component('my-component', {
template: '#my-component',
data() {
return {
cube: '-- no data yet --',
worker: null
};
},
created() {
this.initWorker();
},
beforeDestroy() {
this.destroyWorker();
},
methods: {
initWorker() {
// Here, just for this demo to work, I'm not using an external file
// for the worker. Instead, I use a Blob. It's still a real Worker!
// See https://stackoverflow.com/a/6454685/1913729
const scriptBlob = new Blob(
[ document.querySelector('#worker-script').textContent ],
{ type: "text/javascript" }
);
this.worker = new Worker(window.URL.createObjectURL(scriptBlob));
this.worker.onmessage = e => this.onCubeReady(e.data);
},
destroyWorker() {
this.worker.terminate();
},
scrambleCube() {
this.cube = '-- Scrambling cube... --'
this.worker.postMessage('Gimme a cube');
},
onCubeReady(cube) {
this.cube = cube;
}
}
});
var vm = new Vue({
el: '#app'
});
{{cube}}