Skip to content

Instantly share code, notes, and snippets.

View enkot's full-sized avatar
👌
Focusing

Taras Batenkov enkot

👌
Focusing
View GitHub Profile
@Component
export default class App extends Vue {
@Catch
created() {
return api.getData() // throws Error
.then(data => ...)
}
}
export const catchDecoratorStore = {
handler: null,
setHandler(handler) {
this.handler = handler
}
}
function Catch(target, key, descriptor) {
const originalMethod = descriptor.value
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
// ...
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
const getDataErrorHandler = (error, ctx) => {
ctx.errorMessage = error.message
}
@Component
export default class App extends Vue {
errorMessage = ''
@Catch(getDataErrorHandler)
async getData() {
new Vue({
props: {
id: Number
},
data: { posts: [] },
watch: {
id() {}
},
created() {},
methods: {
// ...
<script>
import { fetchUserPosts } from '@/api'
export default {
props: {
id: Number
},
data() {
return {
const scrollMixin = {
data() {
return {
pageOffset: 0
}
},
mounted() {
window.addEventListener('scroll', this.update)
},
destroyed() {
import { fetchUserPosts } from '@/api'
const withPostsHOC = WrappedComponent => ({
props: WrappedComponent.props,
data() {
return {
postsIsLoading: false,
fetchedPosts: []
}
},
// ...
<script>
export default {
name: 'PostsPage',
mixins: [scrollMixin],
props: {
id: Number,
isLoading: Boolean,
posts: Array,
count: Number