Skip to content

Commit 5c883ec

Browse files
committed
vue component
1 parent a7d8886 commit 5c883ec

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
4+
5+
- [Single Page Applications with Vue.js](#single-page-applications-with-vuejs)
6+
- [Environment Setup - Build Process](#environment-setup---build-process)
7+
- [Project Files](#project-files)
8+
- [Package Management](#package-management)
9+
- [Web Server](#web-server)
10+
- [Web Server - Index.html](#web-server---indexhtml)
11+
- [Webpack - Setup](#webpack---setup)
12+
- [Dev Middleware](#dev-middleware)
13+
- [Hot Reloading](#hot-reloading)
14+
- [Linting](#linting)
15+
- [Single File Components](#single-file-components)
16+
- [Vue Component](#vue-component)
17+
18+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
19+
120
# Single Page Applications with Vue.js
221

322
> Notes from Pluralsight [course](https://app.pluralsight.com/library/courses/vue-js-single-page-applications/table-of-contents)
@@ -126,3 +145,27 @@ npm install [email protected] [email protected] [email protected] eslint-con
126145
Modify [build/webpack.base.config.js](build/webpack.base.config.js) to hook up linting into build process.
127146

128147
Now when server is restarted, build will fail on lint errors.
148+
149+
## Single File Components
150+
151+
Vue uses single file components, each file contains a template for html, a script for js and style for css.
152+
153+
### Vue Component
154+
155+
One way to do things is to declare a component using `Vue.component` method.
156+
157+
On first load, client entry will mount view instance to the div with id of `app` and create an `<app></app>` element.
158+
`<app></app>` element is configured in app.js with a template:
159+
160+
```javascript
161+
Vue.component('app', {
162+
template: `
163+
<div id="app">
164+
...
165+
166+
const app = new Vue({
167+
render: h => h('app')
168+
})
169+
```
170+
171+
Therefore `<app></app>` element will be replaced with app template configuration.

src/app.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
import Vue from 'vue'
22

3-
// Initialize vue
3+
// Use component method to create a component named `app`.
4+
// Template has a div with id of `app` used for initial binding.
5+
// Webapp has navigation section
6+
// Classes are linked with a css library that will be included later.
7+
// Main section is container for site's data.
8+
// Footer section is last.
9+
Vue.component('app', {
10+
template: `
11+
<div id="app">
12+
<nav class="nav has-shadow">
13+
<div class="container">
14+
<a href="/">
15+
<img src="http://bit.ly/vue-img"
16+
alt="Vue SPA" />
17+
</a>
18+
</div>
19+
</nav>
20+
<section class="main-section section"></section>
21+
<footer class="footer">
22+
<div class="container">
23+
<div class="content has-text-centered">
24+
Follow us on
25+
<a href="https://twitter.com/bstavroulakis"
26+
target="_blank">Twitter</a>
27+
</div>
28+
</div>
29+
</footer>
30+
</div>
31+
`
32+
})
33+
34+
// `render` property is a Vue.js function that returns an element
35+
// element to be returned is the `app` element.
436
const app = new Vue({
5-
data: {
6-
hello: 'hi there test is this thing on?'
7-
},
8-
// HMR support so Vue can know how to re-render the section it's mounted on
9-
template: '<div id="app">{{ hello }}</div>'
37+
render: h => h('app')
1038
})
1139

1240
export { app }

src/client-entry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {app} from './app'
22

33
// mount vue instance to a dom element with id of `app`, which must exist in index.html
4+
// i.e. <div id="app"></div>
45
app.$mount('#app')
56

67
// hot module reloading (dev only???)

0 commit comments

Comments
 (0)