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