Skip to content

Commit 12041e2

Browse files
authored
Rewrite the examples with single file component (vuejs#59)
* rewrite the examples with single file component * update the link in readme
1 parent 67ac032 commit 12041e2

File tree

7 files changed

+91
-70
lines changed

7 files changed

+91
-70
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@ Note:
2222

2323
### Example
2424

25-
Following is the example written in Babel. If you are looking for TypeScript version, [it's in the example directory](example/example.ts).
26-
27-
``` js
25+
Following is the example written in Babel. If you are looking for TypeScript version, [it's in the example directory](example/App.vue).
26+
27+
``` vue
28+
<template>
29+
<div>
30+
<input v-model="msg">
31+
<p>prop: {{propMessage}}</p>
32+
<p>msg: {{msg}}</p>
33+
<p>helloMsg: {{helloMsg}}</p>
34+
<p>computed msg: {{computedMsg}}</p>
35+
<button @click="greet">Greet</button>
36+
</div>
37+
</template>
38+
39+
<script>
2840
import Vue from 'vue'
2941
import Component from 'vue-class-component'
3042
3143
@Component({
3244
props: {
3345
propMessage: String
34-
},
35-
template: `
36-
<div>
37-
<input v-model="msg">
38-
<p>prop: {{propMessage}}</p>
39-
<p>msg: {{msg}}</p>
40-
<p>helloMsg: {{helloMsg}}</p>
41-
<p>computed msg: {{computedMsg}}</p>
42-
<button @click="greet">Greet</button>
43-
</div>
44-
`
46+
}
4547
})
46-
class App extends Vue {
48+
export default class App extends Vue {
4749
// initial data
4850
msg = 123
4951
@@ -65,6 +67,7 @@ class App extends Vue {
6567
alert('greeting: ' + this.msg)
6668
}
6769
}
70+
</script>
6871
```
6972

7073
You may also want to check out the `@prop` and `@watch` decorators provided by [vue-property-decorators](https://github.com/kaorun343/vue-property-decorator).

example/App.vue

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<template>
2+
<div>
3+
<input v-model="msg">
4+
<p>prop: {{propMessage}}</p>
5+
<p>msg: {{msg}}</p>
6+
<p>helloMsg: {{helloMsg}}</p>
7+
<p>computed msg: {{computedMsg}}</p>
8+
<button @click="greet">Greet</button>
9+
</div>
10+
</template>
11+
12+
<script lang="ts">
13+
import Vue = require('vue')
14+
import Component from '../lib/index'
15+
16+
@Component({
17+
props: {
18+
propMessage: String
19+
}
20+
})
21+
export default class App extends Vue {
22+
propMessage: string
23+
24+
// inital data
25+
msg: number = 123
26+
27+
// use prop values for initial data
28+
helloMsg: string = 'Hello, ' + this.propMessage
29+
30+
// lifecycle hook
31+
mounted () {
32+
this.greet()
33+
}
34+
35+
// computed
36+
get computedMsg () {
37+
return 'computed ' + this.msg
38+
}
39+
40+
// method
41+
greet () {
42+
alert('greeting: ' + this.msg)
43+
}
44+
}
45+
</script>

example/example.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<div id="el">
9-
<app prop-message="World!"></app>
10-
</div>
8+
<div id="app"></div>
119
<script src="build.js"></script>
1210
</body>
1311
</html>

example/example.ts

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
import Vue = require('vue')
2-
import Component from '../lib/index'
3-
4-
@Component({
5-
props: {
6-
propMessage: String
7-
},
8-
template: `
9-
<div>
10-
<input v-model="msg">
11-
<p>prop: {{propMessage}}</p>
12-
<p>msg: {{msg}}</p>
13-
<p>helloMsg: {{helloMsg}}</p>
14-
<p>computed msg: {{computedMsg}}</p>
15-
<button @click="greet">Greet</button>
16-
</div>
17-
`
18-
})
19-
class App extends Vue {
20-
propMessage: string
21-
22-
// inital data
23-
msg: number = 123
24-
25-
// use prop values for initial data
26-
helloMsg: string = 'Hello, ' + this.propMessage
27-
28-
// lifecycle hook
29-
mounted () {
30-
this.greet()
31-
}
32-
33-
// computed
34-
get computedMsg () {
35-
return 'computed ' + this.msg
36-
}
37-
38-
// method
39-
greet () {
40-
alert('greeting: ' + this.msg)
41-
}
42-
}
2+
import App from './App.vue'
433

444
// mount
455
new Vue({
46-
el: '#el',
47-
components: {
48-
App
49-
}
6+
el: '#app',
7+
render: h => h(App, {
8+
props: { propMessage: 'World' }
9+
})
5010
})

example/sfc.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.vue" {
2+
import Vue = require('vue')
3+
export default typeof Vue
4+
}

example/webpack.config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ module.exports = {
55
filename: 'build.js'
66
},
77
resolve: {
8-
alias: {
9-
vue$: 'vue/dist/vue.common.js'
10-
}
8+
extensions: ['.ts', '.js']
119
},
1210
module: {
1311
rules: [
1412
{
1513
test: /\.ts$/,
1614
exclude: /node_modules|vue\/src/,
17-
loader: 'ts-loader'
15+
loader: 'ts-loader',
16+
options: {
17+
appendTsSuffixTo: [/\.vue$/]
18+
}
19+
},
20+
{
21+
test: /\.vue$/,
22+
loader: 'vue-loader',
23+
options: {
24+
esModule: true
25+
}
1826
}
1927
]
2028
},

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@
4343
"babel-preset-es2015": "^6.18.0",
4444
"chai": "^3.5.0",
4545
"chai-spies": "^0.7.1",
46+
"css-loader": "^0.26.1",
4647
"mocha": "^3.1.2",
4748
"node-libs-browser": "^1.0.0",
4849
"rimraf": "^2.5.4",
4950
"rollup": "^0.37.0",
5051
"rollup-plugin-replace": "^1.1.1",
51-
"ts-loader": "^0.9.5",
52+
"ts-loader": "^1.3.3",
5253
"typescript": "^2.0.6",
5354
"uglify-js": "^2.7.5",
54-
"vue": "^2.1.6",
55-
"webpack": "^2.1.0-beta.27"
55+
"vue": "^2.1.10",
56+
"vue-loader": "^10.0.2",
57+
"vue-template-compiler": "^2.1.10",
58+
"webpack": "^2.2.0"
5659
}
5760
}

0 commit comments

Comments
 (0)