Created
November 27, 2017 09:42
-
-
Save fimars/64d68b1e9c1254b656bc7413caa20d56 to your computer and use it in GitHub Desktop.
A Vue Alert Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const alert = () => { | |
const VmConstructor = Vue.extend({ | |
template: ` | |
<div> | |
<button @click="$emit('yes')">yes</button> | |
<button @click="$emit('no')">no</button> | |
</div> | |
` | |
}) | |
const instance = getAnInstance(VmConstructor) | |
mount(instance) | |
return new Promise((resolve, reject) => { | |
instance.$on('yes', (content = true) => { | |
resolve(content) | |
destroy(instance) | |
}) | |
instance.$on('no', (message = 'cancel') => { | |
reject(createMessage(message)) | |
destroy(instance) | |
}) | |
}) | |
} | |
function createMessage (content) { | |
return `[vv] ${conente}` | |
} | |
function mount(vm) { | |
document.body.appendChild(vm.$el) | |
} | |
function destroy(vm) { | |
document.body.removeChild(vm.$el) | |
vm.$destroy() | |
} | |
function getAnInstance(VmConstructor, propsData = {}) { | |
return new VmConstructor({ | |
el: document.createElement('div'), | |
propsData | |
}) | |
} | |
async function run () { | |
console.log(await alert()) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment