This file contains hidden or 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
const scrollMixin = { | |
data() { | |
return { | |
pageOffset: 0 | |
} | |
}, | |
mounted() { | |
window.addEventListener('scroll', this.update) | |
}, | |
destroyed() { |
This file contains hidden or 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
// ... | |
<script> | |
import { fetchUserPosts } from '@/api' | |
export default { | |
props: { | |
id: Number | |
}, | |
data() { | |
return { |
This file contains hidden or 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
new Vue({ | |
props: { | |
id: Number | |
}, | |
data: { posts: [] }, | |
watch: { | |
id() {} | |
}, | |
created() {}, | |
methods: { |
This file contains hidden or 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
const getDataErrorHandler = (error, ctx) => { | |
ctx.errorMessage = error.message | |
} | |
@Component | |
export default class App extends Vue { | |
errorMessage = '' | |
@Catch(getDataErrorHandler) | |
async getData() { |
This file contains hidden or 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
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 | |
This file contains hidden or 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 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 | |
// ... |
This file contains hidden or 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
export const catchDecoratorStore = { | |
handler: null, | |
setHandler(handler) { | |
this.handler = handler | |
} | |
} | |
function Catch(target, key, descriptor) { | |
const originalMethod = descriptor.value |
This file contains hidden or 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
@Component | |
export default class App extends Vue { | |
@Catch | |
created() { | |
return api.getData() // throws Error | |
.then(data => ...) | |
} | |
} |
This file contains hidden or 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 Catch from './catchDecorator' | |
@Component | |
export default class App extends Vue { | |
@Catch | |
async created() { | |
const data = await api.getData() // throws Error | |
} | |
@Catch |
This file contains hidden or 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
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) | |
} |