Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a vuejs with a method?

Tags:

vue.js

I'm currently trying to wrap my head around how to extend a vuejs instance. Specifically I want to separate an instance, so that I can reuse the base of an instance (the element and the data). I currently have different (laravel/blade) views for adding and editing items (domains), and I want to share a vuejs instance between these two, but I don't want to have the same code (the base) in two places.

Basically, what I'm searching for is the following:

<script type="text/javascript">
    var vue = new Vue({

    el: '#domain',

    data: {
        form: {
            'name'        : '',
            'git_repo'    : '',
            'auto_deploy' : '',
            'create_db'   : ''
        },
        ajaxResponse : ''
    }

    });
</script>

<script type="text/javascript">
    Vue.extend('domain_methods', {

        methods: {

            postDomain: function () {
                this.$http.post('{{ route('domain.store') }}', function (data, status, request) {
                    this.$set('ajaxResponse', data);
                }, {
                    data: this.form
                } ).error(function (data, status, request) {
                    this.$set('ajaxResponse', data);
                });
            }

        }

    });
</script>

But that obviously doesn't work. I just want to use the postDomain() method within the #domain element, without the method being written in the initial creation of the instance.

Thanks in advance!

like image 620
Sebastian Avatar asked Jul 29 '15 19:07

Sebastian


People also ask

How do you extend Vue?

You use Vue. extend to create component definition (called "component constructor" in old documentation) and Vue. component to register it so it can be used in template to actually create component instance. However, everywhere in Vue API where you can pass "component constructor" created by Vue.

How do I extend my class at Vue?

Use them in a class style component: import Component, { mixins } from 'vue-class-component' import { Hello, World } from './mixins' // Use `mixins` helper function instead of `Vue`. // `mixins` can receive any number of arguments. @Component export class HelloWorld extends mixins(Hello, World) { created () { console.

Can you extend Vue components?

Extending Components The pattern we will highlight today is the extends option (part of the Vue options API) that allows us to compose components and share functionality throughout different parts of your UI in a manageable, concise pattern.


1 Answers

Be careful – you are conflating the usage of .extend() with that of .component(). They do very different things. This section of the docs has more information on the differences:

http://vuejs.org/guide/components.html

In this particular case, just declare your top level Vue class via .extend() and then instantiate a subclass of it by using it as a constructor. This gives you inheritance-like behavior.

So, for instance:

<script type="text/javascript">
    var MainVue = Vue.extend({
        data: function () {
            return {
                form: {
                    'name'        : '',
                    'git_repo'    : '',
                    'auto_deploy' : '',
                    'create_db'   : ''
                },
                ajaxResponse : ''
            };
        }
    });
</script>

<script type="text/javascript">
    var secondary_vue = new MainVue({
        el: '#domain',
        methods: {
            postDomain: function () {
                this.$http.post('{{ route('domain.store') }}', function (data, status, request) {
                    this.$set('ajaxResponse', data);
                }, {
                    data: this.form
                } ).error(function (data, status, request) {
                    this.$set('ajaxResponse', data);
                });
            }
        }
    });
</script>
like image 164
David K. Hess Avatar answered Oct 19 '22 00:10

David K. Hess