computed: {
now: function () {
return Date.now()
}
}
computed: {
now: function () {
return this.ticker > 0 ? Date.now() : ''
}
}
// analog to
new Vue({ ... })
import Vue from 'vue';
export const EventBus = new Vue();
// Import the EventBus we just created.
import { EventBus } from './event-bus.js';
// emitter
EventBus.$emit('drawer::setState', true)
// receiver
EventBus.$on('drawer::setState', (state) => {
if (state) {
this.openDrawer()
return
}
this.closeDrawer()
})
// receiver
EventBus.$off('drawer::setState')
<template>
<button @click="click">Click me</button>
</template>
<script>
export default {
methods: {
click () {
window.alert('Clica ni mim!')
}
}
}
</script>
import AppButton from './AppButton'
export default {
extends: AppButton,
methods: {
click() {
window.alert('Clica aqui no pai!')
}
}
}
export default {
methods: {
send() {
window.alert('O pai resolveu já!')
}
}
}
<template>
<button @click="click">Click me</button>
</template>
<script>
export default {
methods: {
click () {
// use here to check in runtinme
this.checkHandler()
this.handler()
},
checkHandler () {
if (this.handler) {
return
}
throw new Error('The "handler" method is required')
}
},
created () {
// check on create component
this.checkHandler()
}
}
</script>
import AppButton from "./AppButton";
export default {
extends: AppButton,
methods: {
handler() {
window.alert("Ai sim ein?!");
}
}
};
<template>
<button @click="click">Click me</button>
</template>
<script>
export default {
$handler: undefined,
methods: {
click() {
this.$options.$handler.execute();
},
checkHandler() {
if (this.$options.$handler) {
return;
}
throw new Error('The "handler" method is required');
}
},
created() {
this.checkHandler();
}
};
</script>
import AppButton from "./AppButton";
export default {
extends: AppButton,
$handler: {
execute() {
window.alert("Ai sim ein?!");
}
}
};
<template>
...
</template>
<script>
export default {
created () {
if (!this.prop) {
return
}
this.unWatch = this.$watch(this.prop, () => this.$emit('it-works'))
}
}
</script>
<template>
...
</template>
<script>
export default {
watch: {
prop: {
immediate: true,
handler () {
this.$emit('it-works')
}
}
}
}
</script>
<script>
export default {
watch: {
prop: {
deep: true,
handler () {
this.$emit('it-works')
}
}
}
}
</script>
<script>
export default {
watch: {
'$router.fullPath' () {
this.$emit('crazy-day')
}
},
created () {
this.unWatch = this.$watch('orderItem.product.price', ...)
}
}
</script>
<form class="grid">
<AppInput
class="width-6"
label="Name"
v-model="record.name"
/>
<AppInput
class="width-6"
label="Last Name"
v-model="record.lastName"
/>
<AppSelect
class="width-12"
label="Estado"
v-model="record.state"
v-bind="schema.state"
@input="onInputState"
/>
<AppSelect
class="width-12"
label="Cidade"
v-model="record.city"
v-bind="schema.city"
/>
</form>
<template>
<div class="cell">
<label>{{ label }}</label>
<input v-bind="bind" :value="value" @input="input">
</div>
</template>
<script>
export default {
props: ["label", "value"],
computed: {
bind() {
return { ...this.$props, ...this.$attrs };
}
},
methods: {
input($event) {
this.$emit("input", $event.target.value);
}
}
};
</script>
export default {
name: "AppField",
props: ['label', 'value'],
computed: {
bind() {
return { ...this.$props, ...this.$attrs }
}
},
methods: {
input($event) {
this.$emit('input', this.parseValue($event))
},
parseValue($event) {
return $event.target.value
}
}
}
<template>
<div class="cell">
<label>{{ label }}</label>
<input
v-bind="bind"
:value="value"
@input="input"
/>
</div>
</template>
<script>
import AppField from "./AppField";
export default {
extends: AppField,
name: "AppInput"
};
</script>
<template>
<div class="cell">
<label>{{ label }}</label>
<select v-bind="bind" v-model="model" @input="input">
<option
v-for="(option, index) in options"
:value="option[optionValue]"
>
{{ option[optionLabel] }}
</option>
</select>
</div>
</template>
<script>
import AppField from "./AppField";
export default {
extends: AppField,
methods: {
parseValue($event) {
return this.getOption($event.target.value);
},
// ...
},
watch: {
value: {
immediate: true,
handler(value) {
this.model = value[this.optionValue];
}
}
}
};
</script>
import AppForm from "./AppForm";
import { getStates, getPerson, getCities } from "../services";
export default {
extends: AppForm,
name: "MyForm",
record: { name: undefined, lastName: undefined, state: undefined, city: undefined },
schema: { ... },
computed: {
recordComputed() {
return {
fullName: `${this.record.name} ${this.record.lastName}`
};
}
},
methods: {
async initialize() {
this.schema.state.options = await getStates();
this.record = await getPerson();
}
},
watch: {
async "record.state"(value) {
this.schema.city.options = await getCities(value);
}
}
};
import AppInput from './AppInput'
import AppSelect from './AppSelect'
export default {
name: 'AppForm',
components: { AppInput, AppSelect },
data: () => ({ record: this.$options.record, schema: this.$options.schema }),
computed: {
payload() {
return { ...this.record, ...this.recordComputed }
},
recordComputed() {
return {}
}
},
methods: {
initialize() {}
},
created() {
this.initialize()
}
}
<template>
<div class="AppForm">
<form class="grid">
<!-- ... -->
<div class="cell buttons">
<button @click="save">Save Me</button>
</div>
</form>
<pre>{{ payload }}</pre>
</div>
</template>
<script>
import AppForm from "./AppForm";
import MixinOperations from "./MixinOperations";
import { getStates, getPerson, getCities } from "../services";
export default {
extends: AppForm,
mixins: [MixinOperations],
name: "MyForm",
// ...
};
</script>