How to create custom functions for Vue JS such as created() hook?
P粉512729862
P粉512729862 2023-09-04 09:39:05
0
1
348

How should I create a plugin that adds a function named struct (like a created() hook) to all components?

Also, I want my plugin to have access to the structure return value.

export default { structure() { // Access to context } } 

I have to mention that I use Inertia JS.

P粉512729862
P粉512729862

reply all (1)
P粉848442185

You can use VueMixinsorCombinables.

Both can provide you with some shared functions and variables. But I don't know how to define new hooks in Vue like create() . I have to start your function in created() myself. Of course, you can use mixins to override existing Vue hooks.

Mixin is very convenient, butis no longer recommended

There is nocreated()in the Composition API, so you must useonBeforeMount()oronMounted()

This is a very basic example using both techniques

const { createApp, ref, onBeforeMount } = Vue; const myMixin = { created() { console.log('myMixin: created()') } } const myComposable = () => { onBeforeMount(() => { console.log('myComposable: onBeforeMount()') }) const myFunction = () => console.log('myFunction()') return { myFunction } } const App = { setup() { const { myFunction } = myComposable() return { myFunction } }, mixins: [myMixin] } const app = createApp(App) app.mount('#app')
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!