Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add tsx render function example
  • Loading branch information
decadef20 committed Jan 7, 2018
commit 5f78a2d1baf33ab3d740baa27d12b4a32a5f9e3e
8 changes: 8 additions & 0 deletions example/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
["env", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this preset and transform-decorators-legacy since we already transpile the code by TypeScript. Can we remove them from babelrc?

"modules": false
}]
],
"plugins": ["transform-vue-jsx","transform-decorators-legacy"]
}
7 changes: 6 additions & 1 deletion example/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
<p>computed msg: {{computedMsg}}</p>
<button @click="greet">Greet</button>
<hello ref="helloComponent"></hello>
<World :title="title"/>
</div>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from '../lib/index'
import Hello from './Hello.vue';
import World from './World.tsx'

@Component({
props: {
propMessage: String
},
components: {
Hello
Hello,
World
}
})
export default class App extends Vue {
Expand All @@ -33,6 +36,8 @@ export default class App extends Vue {
// use prop values for initial data
helloMsg: string = 'Hello, ' + this.propMessage

title: string = 'tsx render function'

// lifecycle hook
mounted () {
this.greet()
Expand Down
31 changes: 31 additions & 0 deletions example/World.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Vue from 'vue'
import Component from '../lib/index'

@Component({
props:{
title:{
type:String,
required:true
}
}
})
export default class World extends Vue {
title:string
content:string = 'default content'
get computedContent () {
return 'computed ' + this.content
}

clearContent() {
this.content = ''
}
render(h) {
return <div >
<h1>{this.title}</h1>
Copy link
Member

@HerringtonDarkholme HerringtonDarkholme Dec 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC tsx requires additional type declaration under strict: true flag. Will this compile?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify the render function and the component definition more? I think it could have only one simple element since we just want to show tsx is available in this component. props/methods/computed shouldn't be there since they just complicate the example, IMO.

<input type="text" value={this.content} onInput={ (evt:Event)=> this.content = (evt.target as HTMLInputElement).value} />
<p>content: {this.content} </p>
<p>computed content: {this.computedContent} </p>
<button onClick={this.clearContent}>Clear content</button>
</div>
}
}
3 changes: 2 additions & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"experimentalDecorators": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true
"noUnusedParameters": true,
"jsx": "preserve"
},
"include": [
"./**/*.ts"
Expand Down
14 changes: 13 additions & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
filename: 'build.js'
},
resolve: {
extensions: ['.ts', '.js']
extensions: ['.ts', '.js','tsx']
},
module: {
rules: [
Expand All @@ -17,6 +17,18 @@ module.exports = {
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.tsx$/,
exclude: /node_modules|vue\/src/,
loader:[
{
loader: "babel-loader"
},
{
loader: "ts-loader",
}
]
},
{
test: /\.vue$/,
loader: 'vue-loader',
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
"@types/chai": "^4.0.1",
"@types/mocha": "^2.2.41",
"babel-core": "^6.25.0",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-loader": "^7.1.1",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"chai": "^4.0.2",
"css-loader": "^0.28.4",
Expand Down