| 1 |
<link rel=stylesheet href='./style.css'> |
| 2 |
<script src="./vue_3.1.5.js"></script> |
| 3 |
|
| 4 |
<div id=root><div></div></div> |
| 5 |
|
| 6 |
<script> |
| 7 |
|
| 8 |
window.mountApp = element => { |
| 9 |
const app = Vue.createApp({ |
| 10 |
template: ` |
| 11 |
<app-header :bookCount='books.length'></app-header> |
| 12 |
<new-book @newbook='addNewBook'></new-book> |
| 13 |
<book-list :books='books'></book-list> |
| 14 |
<button-grid></button-grid> |
| 15 |
<div ref="mountPoint"></div> |
| 16 |
`, |
| 17 |
|
| 18 |
data() { |
| 19 |
return { |
| 20 |
books: [ |
| 21 |
{name: 'Pride and Prejudice' }, |
| 22 |
{name: 'To Kill a Mockingbird' }, |
| 23 |
{name: 'The Great Gatsby' }, |
| 24 |
], |
| 25 |
}; |
| 26 |
}, |
| 27 |
|
| 28 |
methods: { |
| 29 |
addNewBook(name) { |
| 30 |
this.books.push({name}); |
| 31 |
} |
| 32 |
}, |
| 33 |
}); |
| 34 |
|
| 35 |
app.component('app-header', { |
| 36 |
template: ` |
| 37 |
<h1>vuejs@${Vue.version}</h1> |
| 38 |
<h3>Reading List: {{ bookCount }}</h3> |
| 39 |
`, |
| 40 |
props: [ 'bookCount' ], |
| 41 |
}); |
| 42 |
|
| 43 |
app.component('new-book', { |
| 44 |
data() { |
| 45 |
return { |
| 46 |
name: '', |
| 47 |
} |
| 48 |
}, |
| 49 |
template: ` |
| 50 |
<input v-model='name' @keypress.enter='onNewBook'><button @click='onNewBook'>new book</button> |
| 51 |
`, |
| 52 |
emits: ['newbook'], |
| 53 |
methods: { |
| 54 |
onNewBook() { |
| 55 |
this.$emit('newbook', this.name); |
| 56 |
} |
| 57 |
}, |
| 58 |
}); |
| 59 |
app.component('book-item', { |
| 60 |
template: ` |
| 61 |
<div> |
| 62 |
{{ name }} |
| 63 |
</div> |
| 64 |
`, |
| 65 |
props: ['name'], |
| 66 |
}); |
| 67 |
|
| 68 |
app.component('book-list', { |
| 69 |
props: ['books'], |
| 70 |
template: ` |
| 71 |
<ol> |
| 72 |
<li v-for='book in books' :key='book.name'> |
| 73 |
<book-item :name='book.name'></book-item> |
| 74 |
</li> |
| 75 |
</ol> |
| 76 |
`, |
| 77 |
}); |
| 78 |
|
| 79 |
app.component('color-button', { |
| 80 |
props: { |
| 81 |
color: String, |
| 82 |
enabled: Boolean, |
| 83 |
nested: { |
| 84 |
index: Number, |
| 85 |
value: Number, |
| 86 |
}, |
| 87 |
}, |
| 88 |
template: ` |
| 89 |
<button :disabled='enabled' :class='color'>button {{nested.index}}</button> |
| 90 |
`, |
| 91 |
}); |
| 92 |
|
| 93 |
app.component('button-grid', { |
| 94 |
render() { |
| 95 |
const buttons = []; |
| 96 |
const ColorButton = Vue.resolveComponent('color-button'); |
| 97 |
for (let i = 0; i < 9; ++i) { |
| 98 |
buttons.push(Vue.h(ColorButton, { |
| 99 |
color: ['red', 'green', 'blue'][i % 3], |
| 100 |
enabled: i % 2 === 0, |
| 101 |
nested: { |
| 102 |
index: i, |
| 103 |
value: i + 0.1, |
| 104 |
} |
| 105 |
}, null)); |
| 106 |
}; |
| 107 |
return buttons; |
| 108 |
} |
| 109 |
}); |
| 110 |
return app.mount(element); |
| 111 |
} |
| 112 |
|
| 113 |
window.app = window.mountApp(document.querySelector('#root div')); |
| 114 |
window.mountNestedApp = () => window.mountApp(window.app.$refs.mountPoint); |
| 115 |
|
| 116 |
</script> |