feat: improve vue-class-component example

This commit is contained in:
Clark Du 2017-10-19 16:56:00 +08:00
parent f8bc7e2383
commit 2f670c5ef7
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 72 additions and 29 deletions

View File

@ -0,0 +1,40 @@
<template>
<div>
<input v-model="msg">
<p>msg: {{msg}}</p>
<p>env: {{env}}</p>
<p>computed msg: {{computedMsg}}</p>
<button @click="greet">Greet</button>
<p><nuxt-link to="/about">About page</nuxt-link></p>
</div>
</template>
<script>
import Vue from 'vue'
import Component from 'nuxt-class-component'
@Component({
props: {
env: String
}
})
export default class Base extends Vue {
// initial data
msg = 123
// lifecycle hook
mounted () {
this.greet()
}
// computed
get computedMsg () {
return 'computed ' + this.msg
}
// method
greet () {
console.log('base greeting: ' + this.msg)
}
}
</script>

View File

@ -0,0 +1,23 @@
<template>
<div>
<input v-model="msg">
<p>msg: {{msg}}</p>
<p>env: {{env}}</p>
<p>computed msg: {{computedMsg}}</p>
<button @click="greet">Greet</button>
<p><nuxt-link to="/about">About page</nuxt-link></p>
</div>
</template>
<script>
import Component from 'nuxt-class-component'
import Base from '@/components/Base'
@Component
export default class Child extends Base {
// override parent method
greet () {
console.log('child greeting: ' + this.msg)
}
}
</script>

View File

@ -1,3 +1,6 @@
<template>
<h1>About</h1>
<div>
<h1>About</h1>
<p><nuxt-link to="/">Home page</nuxt-link></p>
</div>
</template>

View File

@ -1,41 +1,18 @@
<template>
<div>
<input v-model="msg">
<p>msg: {{msg}}</p>
<p>env: {{env}}</p>
<p>computed msg: {{computedMsg}}</p>
<button @click="greet">Greet</button>
<p><nuxt-link to="/about">About page</nuxt-link></p>
</div>
<Child :env="env" ></Child>
</template>
<script>
import Vue from 'vue'
import Component from 'nuxt-class-component'
import Child from '@/components/Child'
@Component()
@Component({
components: { Child }
})
export default class App extends Vue {
// initial data
msg = 123
asyncData ({ req }) {
return { env: req ? 'server' : 'client' }
}
// lifecycle hook
mounted () {
this.greet()
}
// computed
get computedMsg () {
return 'computed ' + this.msg
}
// method
greet () {
console.log('greeting: ' + this.msg)
}
}
</script>