Created
July 25, 2017 15:38
-
-
Save markselby9/e432f457cf518cf620ace76ab0fe2226 to your computer and use it in GitHub Desktop.
vue decorator using 'vue-class-component'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<input v-model="msg"> | |
<!--<p>prop: {{propMessage}}</p>--> | |
<p>msg: {{msg}}</p> | |
<!--<p>helloMsg: {{helloMsg}}</p>--> | |
<p>computed msg: {{computedMsg}}</p> | |
<button @click="greet">Greet</button> | |
</div> | |
</template> | |
<script> | |
import Vue from 'vue' | |
import Component from 'vue-class-component' | |
import { test, createDec } from './decorator'; | |
@Component | |
export default class App extends Vue { | |
// initial data | |
msg = 123 | |
// use prop values for initial data | |
// helloMsg = 'Hello, ' + this.propMessage | |
// lifecycle hook | |
mounted() { | |
this.greet() | |
} | |
// @test(123) | |
@createDec | |
created() { | |
console.log('old created'); | |
} | |
// computed | |
get computedMsg() { | |
return 'computed ' + this.msg | |
} | |
// method | |
greet() { | |
console.log('greeting: ' + this.msg) | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createDecorator } from 'vue-class-component'; | |
export function test(id) { | |
console.log('evaluated', id); | |
return (target, property, descriptor) => { | |
console.log('==='); | |
console.log(target, property, descriptor); | |
console.log('executed', id); | |
} | |
} | |
export const createDec = createDecorator((options, key) => { | |
// component options should be passed to the callback | |
// and update for the options object affect the component | |
options.foo = 'bar'; | |
const originalCreated = options.created; | |
options.created = function () { | |
console.log('new created'); | |
originalCreated.apply(this); | |
}; | |
console.log(options, key, '==='); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment