Skip to content

Commit c094572

Browse files
committed
Refactor into a single file component.
Note the use of the browser field in the package.json to avoid the error "Failed to mount component: template or render function not defined. (found in root instance)" due to using the default runtime build. See https://vuejs.org/guide/installation.html#Standalone-vs-Runtime-only-Build .
1 parent 1a46dd1 commit c094572

File tree

6 files changed

+60
-43
lines changed

6 files changed

+60
-43
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.DS_Store
22
node_modules/
3-
dist/build.js
3+
dist/app.js
44
npm-debug.log

dist/.gitkeep

Whitespace-only changes.

index.html

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,8 @@
77
</head>
88
<body>
99
<div id="app">
10-
<select v-model="type">
11-
<option value="dice">Board Game</option>
12-
<option value="spades">Card Game</option>
13-
</select>
14-
<input v-model="game" @keyup.enter="addGame">
15-
<button @click="addGame">Add</button>
16-
<p v-if="empty">Zarro Boords!</p>
17-
<ul v-else>
18-
<li v-for="g in games">
19-
<span :class="icon(g.type)"></span>
20-
{{ g.game }}
21-
</li>
22-
</ul>
10+
<board-library></board-library>
2311
</div>
24-
<script src="node_modules/vue/dist/vue.js"></script>
25-
<script>
26-
var app = new Vue({
27-
el: '#app',
28-
data: {
29-
game: '',
30-
type: 'dice',
31-
games: []
32-
},
33-
computed: {
34-
empty: function() {
35-
return this.games.length == 0
36-
}
37-
},
38-
methods: {
39-
addGame: function () {
40-
this.games.push({
41-
game: this.game,
42-
type: this.type
43-
})
44-
},
45-
icon: function (type) {
46-
return 'icon-' + type
47-
}
48-
}
49-
})
50-
</script>
12+
<script src="dist/app.js"></script>
5113
</body>
5214
</html>

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"author": "Andy Wood <andy@ideaflip.com>",
55
"private": true,
66
"scripts": {
7-
"watchify": "watchify -vd -p browserify-hmr -e src/main.js -o dist/build.js",
7+
"watchify": "watchify -vd -p browserify-hmr -e src/main.js -o dist/app.js",
88
"serve": "http-server -o -s -c 1 -a localhost",
99
"dev": "npm-run-all --parallel watchify serve",
10-
"build": "cross-env NODE_ENV=production browserify -g envify src/main.js | uglifyjs -c warnings=false -m > dist/build.js"
10+
"build": "cross-env NODE_ENV=production browserify -g envify src/main.js | uglifyjs -c warnings=false -m > dist/app.js"
1111
},
1212
"dependencies": {
1313
"vue": "^2.0.1"
@@ -26,6 +26,9 @@
2626
"vueify": "^9.1.0",
2727
"watchify": "^3.4.0"
2828
},
29+
"browser": {
30+
"vue": "vue/dist/vue.js"
31+
},
2932
"browserify": {
3033
"transform": [
3134
"vueify",

src/BoardLibrary.vue

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<template>
2+
<div>
3+
<select v-model="type">
4+
<option value="dice">Board Game</option>
5+
<option value="spades">Card Game</option>
6+
</select>
7+
<input v-model="game" @keyup.enter="addGame">
8+
<button @click="addGame">Add</button>
9+
<p v-if="empty">Zarro Boords!</p>
10+
<ul v-else>
11+
<li v-for="g in games">
12+
<span :class="icon(g.type)"></span>
13+
{{ g.game }}
14+
</li>
15+
</ul>
16+
</div>
17+
</template>
18+
19+
<script>
20+
module.exports = {
21+
data: function() {
22+
return {
23+
game: '',
24+
type: 'dice',
25+
games: []
26+
}
27+
},
28+
computed: {
29+
empty: function() {
30+
return this.games.length == 0
31+
}
32+
},
33+
methods: {
34+
addGame: function () {
35+
this.games.push({
36+
game: this.game,
37+
type: this.type
38+
})
39+
},
40+
icon: function (type) {
41+
return 'icon-' + type
42+
}
43+
}
44+
}
45+
</script>

src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var Vue = require('vue')
2+
var BoardLibrary = require('./BoardLibrary.vue')
3+
4+
Vue.component('board-library', BoardLibrary)
5+
var app = new Vue({
6+
el: '#app'
7+
})

0 commit comments

Comments
 (0)