Skip to content

Commit 6095c0f

Browse files
decadef20ktsn
authored andcommitted
add tsx render function example (#212)
* add tsx render function example * add jsx typing * simplify the render function example
1 parent e726999 commit 6095c0f

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

example/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-vue-jsx"]
3+
}

example/App.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
<p>computed msg: {{computedMsg}}</p>
88
<button @click="greet">Greet</button>
99
<hello ref="helloComponent"></hello>
10+
<World />
1011
</div>
1112
</template>
1213

1314
<script lang="ts">
1415
import Vue from 'vue'
1516
import Component from '../lib/index'
1617
import Hello from './Hello.vue';
18+
import World from './World.tsx'
1719
1820
@Component({
1921
props: {
2022
propMessage: String
2123
},
2224
components: {
23-
Hello
25+
Hello,
26+
World
2427
}
2528
})
2629
export default class App extends Vue {
@@ -33,6 +36,7 @@ export default class App extends Vue {
3336
// use prop values for initial data
3437
helloMsg: string = 'Hello, ' + this.propMessage
3538
39+
3640
// lifecycle hook
3741
mounted () {
3842
this.greet()

example/World.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Vue ,{ CreateElement } from 'vue'
2+
import Component from '../lib/index'
3+
4+
@Component
5+
export default class World extends Vue {
6+
7+
render(h: CreateElement) {
8+
return <h1 > tsx render function </h1>
9+
}
10+
}

example/jsx.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Vue, { VNode } from "vue";
2+
declare global {
3+
namespace JSX {
4+
interface Element extends VNode {}
5+
interface ElementClass extends Vue {}
6+
interface IntrinsicElements {
7+
[elem: string]: any;
8+
}
9+
}
10+
}

example/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"experimentalDecorators": true,
1111
"strict": true,
1212
"noUnusedLocals": true,
13-
"noUnusedParameters": true
13+
"noUnusedParameters": true,
14+
"jsx": "react",
15+
"jsxFactory": "h"
1416
},
1517
"include": [
1618
"./**/*.ts"

example/webpack.config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
filename: 'build.js'
66
},
77
resolve: {
8-
extensions: ['.ts', '.js']
8+
extensions: ['.ts', '.js','tsx']
99
},
1010
module: {
1111
rules: [
@@ -17,6 +17,18 @@ module.exports = {
1717
appendTsSuffixTo: [/\.vue$/]
1818
}
1919
},
20+
{
21+
test: /\.tsx$/,
22+
exclude: /node_modules|vue\/src/,
23+
loader:[
24+
{
25+
loader: "babel-loader"
26+
},
27+
{
28+
loader: "ts-loader",
29+
}
30+
]
31+
},
2032
{
2133
test: /\.vue$/,
2234
loader: 'vue-loader',

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@
3939
"@types/chai": "^4.0.1",
4040
"@types/mocha": "^2.2.41",
4141
"babel-core": "^6.25.0",
42+
"babel-helper-vue-jsx-merge-props": "^2.0.3",
4243
"babel-loader": "^7.1.1",
44+
"babel-plugin-syntax-jsx": "^6.18.0",
4345
"babel-plugin-transform-class-properties": "^6.24.1",
4446
"babel-plugin-transform-decorators-legacy": "^1.3.4",
47+
"babel-plugin-transform-vue-jsx": "^3.5.0",
48+
"babel-preset-env": "^1.6.1",
4549
"babel-preset-es2015": "^6.24.1",
4650
"chai": "^4.0.2",
4751
"css-loader": "^0.28.4",

0 commit comments

Comments
 (0)