mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Merge pull request #1890 from clarkdo/vue-class-component-example
feat: improve vue-class-component example
This commit is contained in:
commit
56c4cd25d9
40
examples/vue-class-component/components/Base.vue
Normal file
40
examples/vue-class-component/components/Base.vue
Normal 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>
|
23
examples/vue-class-component/components/Child.vue
Normal file
23
examples/vue-class-component/components/Child.vue
Normal 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>
|
@ -4,7 +4,7 @@ module.exports = {
|
||||
plugins: ['transform-decorators-legacy', 'transform-class-properties']
|
||||
},
|
||||
extend (config) {
|
||||
config.resolve.alias['nuxt-class-component'] = '~plugins/nuxt-class-component'
|
||||
config.resolve.alias['nuxt-class-component'] = '@/plugins/nuxt-class-component'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1>About</h1>
|
||||
<p><nuxt-link to="/">Home page</nuxt-link></p>
|
||||
</div>
|
||||
</template>
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user