2017-03-06 8 views
0

通知機能を他のJSファイルから呼び出したいのですが、常にundefinedを返します。他のjsファイルからJavascript(Vue)コール関数が返されます。未定義

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

    }); 
} 

notification.js

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> 

いつもprice > currentPointと呼ばれることがありますので、いつも返信しますUncaught ReferenceError: notification is not defined

LaravelMixを使用してコンパイルします。

+0

に呼び出すことができます

export default user 

。だからそれは返されません –

答えて

0

はそれを解決します。私はwindow.notification = notificationを追加する必要がありますので、どこでも呼び出せます。

0

あなたがちょうどあなたのHTMLに次の操作を行う必要がありそうです:

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

    }); 
} 

window.notification = notification; 

notification.js

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

私はLaravelを使用します。私のコードはあなたのようにまったく同じようにレンダリングされます – ssuhat

0

ウィンドウに物を置くのは避ける方が良いです。

ウェブパックでVueを使用しているようです。あなたがいない場合、これは動作しません。 ES6モジュールを使用している場合は、必要な場所にアクセスできるようにすることをお勧めします。あなたのnotification.js内

ファイル:あなたはあなたの通知方法は、任意のreturn文を持っていない

import notification from 'notification' 
関連する問題