1

How can i extend another component ??

My main component

MainComponent.vue

<template>
    <div>
        empty
    </div>
</template>

<script>
    export default {
        methods: {
            sendAlert(msg) {
                alert(msg);
            }
        }
    }
</script>

and second component;

SecondComponent.vue

<template>
    <div>
        <button @click="MainComponent.methods.sendAlert('first try')"></button>
        <!--OR-->
        <button @click="sendAlert('second try')"></button>
    </div>
</template>

<script>

    import MainComponent from "./MainComponent.vue"

    export default {
        extends: { MainComponent },
        methods: {
            ...MainComponent.methods
        }
    }
</script>

i can't access methods. How can i do it?

Note:(i dont want this)

<template>
    <div>
        <button @click="sendAlert('second try')"></button>
    </div>
</template>

<script>

    import MainComponent from "./MainComponent.vue"

    export default {
        extends: { MainComponent },
        methods: {
            Message(msg) {
                MainComponent.methods.sendAlert(msg);
            }
        }
    }
</script>

So I have one or more methods in a component. How can I use these methods in another component?

2
  • Every framework has its own way of doing things, you cannot go and ask libraries to do it your way. That said, if you want to share functions, you should use mixins, that is the way it is done in land of Vue. There is no real concept of inheritance and extensions of another view in Vue. Commented Apr 12, 2021 at 11:45
  • Put the sendAlert method in some file .e.g. /use/alert.js and then import it in components that need it like import { sendAlert } from './use/alert'. Commented Apr 12, 2021 at 11:45

2 Answers 2

2

If you want to use same method or even same attributes you can create mixins.js file

export const YouMixins = {
    methods: {
        sendAlert(msg) {
            alert(msg);
        }    
    }
}

Then in your second component

<template>
    <div>
        <button @click="sendAlert('first try')"></button>
        <!--OR-->
        <button @click="sendAlert('second try')"></button>
    </div>
</template>

<script>
    import { YouMixins } from "./mixins.js"

    export default {
        mixins: [YouMixins],
    }
</script>

Note you can pass not methods only even computed, data, life cycles and more

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use this to access MainComponent method :

<template>
    <div>
        <button @click="sendAlert('second try')"></button>
    </div>
</template>

<script>

    import MainComponent from "./MainComponent.vue"

    export default {
        extends: { MainComponent },
        methods: {
            Message(msg) {
                this.sendAlert(msg);
            }
        }
    }
</script>

2 Comments

I don't want to use this. There is no problem when there is a method, but why should I write over and over again when there are 10 methods?
what about extends: MainComponent , or using mixins

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.