Skip to content

Instantly share code, notes, and snippets.

View enkot's full-sized avatar
👌
Focusing

Taras Batenkov enkot

👌
Focusing
View GitHub Profile
const scrollMixin = {
data() {
return {
pageOffset: 0
}
},
mounted() {
window.addEventListener('scroll', this.update)
},
destroyed() {
// ...
<script>
import { fetchUserPosts } from '@/api'
export default {
props: {
id: Number
},
data() {
return {
new Vue({
props: {
id: Number
},
data: { posts: [] },
watch: {
id() {}
},
created() {},
methods: {
const getDataErrorHandler = (error, ctx) => {
ctx.errorMessage = error.message
}
@Component
export default class App extends Vue {
errorMessage = ''
@Catch(getDataErrorHandler)
async getData() {
function Catch(localHandler) {
return function(target, key, descriptor) {
const originalMethod = descriptor.value
descriptor.value = async function(...args) {
try {
return await originalMethod.apply(this, args)
} catch (error) {
const { handler } = catchDecoratorStore
import Catch, { catchDecoratorStore } from './catchDecorator'
catchDecoratorStore.setHandler(error => Toast.error(error.message))
@Component
export default class App extends Vue {
@Catch
async created() {
const data = await api.getData() // throws Error
// ...
export const catchDecoratorStore = {
handler: null,
setHandler(handler) {
this.handler = handler
}
}
function Catch(target, key, descriptor) {
const originalMethod = descriptor.value
@Component
export default class App extends Vue {
@Catch
created() {
return api.getData() // throws Error
.then(data => ...)
}
}
import Catch from './catchDecorator'
@Component
export default class App extends Vue {
@Catch
async created() {
const data = await api.getData() // throws Error
}
@Catch
function Catch(target, key, descriptor) {
const originalMethod = descriptor.value
descriptor.value = async function(...args) {
try {
return await originalMethod.apply(this, args)
} catch (error) {
console.warn(error.message)
Toast.error(error.message)
}