0

I want to call my notification function from other JS file but it always return undefined.

notification.js

function notification(message, level = 'success') {
    Lobibox.notify(level, {
        delayIndicator: false,
        msg: message

    });
}

addToCart.vue

<template>
    <div>
    <button class="button dark small" @click="addToCart(menu, price)">
        <i class="fa fa-cutlery fa-lg m-right-5"></i>
        Pilih Menu
    </button>
    </div>
</template>

<script>
    export default{
        props: ['menu', 'price'],

        methods:{
            addToCart(menu, price){
                const currentPoint = $('#current_point').val();

                if(price > currentPoint){
                    return notification('Point is not enough', 'error')
                }


            }
        }
    }
</script>

app.js

Vue.component('addtocart', require('./components/AddToCart.vue'));
new Vue({
    el: '#app'
});

html:

<script src="{{ asset('js/notification.js') }}"></script>
<script src="{{ asset('js/app.js') }}"></script>

Every time price > currentPoint get called it always return me Uncaught ReferenceError: notification is not defined.

I compile it using LaravelMix.

Any solution?

1
  • Your notification method does not have any return statement. So it returns undefined Commented Mar 6, 2017 at 7:45

3 Answers 3

1

notification.js

function notification(message, level = 'success') {
    Lobibox.notify(level, {
        delayIndicator: false,
        msg: message

    });
}

window.notification = notification;

Solve it. I need to add window.notification = notification so I can call it everywhere.

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

Comments

0

It seems you just need to do following in your HTML:

<script src="js/notification.js"></script>
<script src="js/app.js"></script>

1 Comment

I use Laravel. My code will render exactly the same like yours
0

it's better to avoid putting things in window.

It looks like you're using the Vue with webpack. If you're not, then this won't work. If you are, you're better off using ES6 modules to make sure you have access to things where you need them.

Within your notification.js file:

export default user

Which you can then call on

import notification from 'notification'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.