<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>lesson 21</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
currentItem: 'input-item'
}
},
methods: {
handleClick() {
this.currentItem === 'input-item' ? this.currentItem = 'common-item' : this.currentItem = 'input-item';
}
},
template: `
<keep-alive>
<component :is="currentItem" />
</keep-alive>
<input-item v-show="currentItem === 'input-item'" />
<common-item v-show="currentItem === 'common-item'" />
<button @click="handleClick">change</button>
<common-item />
<async-common-item />
`
});
app.component('common-item', {
template:`
<div>hello-world</div>
`
});
app.component('input-item', {
template:
`<div>
<input />
</div>`
});
app.component('async-common-item',Vue.defineAsyncComponent(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
template:`<div>this is an async component</div>`
})
}, 4000);
})
}))
app.mount('#root');
</script>
</html>