javascript - vue組件通過eventBus通信時,報錯a.$on is not a function
問題描述
登錄組件import bus from '@/components/eventBus'export default { name: ’login’, data () {
return { navcard:[’首頁’,’工作室簡介’,’人員分工’,’項目計劃’,’重點故障分析’,’技能創新’,’帶徒傳技’,’技能培訓’,’技術交流論壇’], username:’’, userpassword:’’, loginstate:’3’}},methods:{login:function(){ this.$http.get('http://localhost/web/njndc1/api.php',{params:{ ’username’:this.username,’userpassword’:this.userpassword,’action’:’login’ }}).then(function (res) {// 處理成功的結果 this.loginstate=res.body; console.log(this.loginstate) if(res.body==1){ var self=this; this.$router.push(’/mainpage’) bus.$emit('login_loginstae',’success’) }else{ bus.$emit('login_loginstae',’failed’) } },function (res) {// 處理失敗的結果alert(’程序錯誤’)} );}
}}</script>主頁面組件<script>import bus from '@/components/eventBus'export default { name: ’login’, data () {
return { navcard:[’首頁’,’工作室簡介’,’人員分工’,’項目計劃’,’重點故障分析’,’技能創新’,’帶徒傳技’,’技能培訓’,’技術交流論壇’], username:’’, userpassword:’’, msg:’’}},methods:{
}, created(){
var self=this; bus.$on('login_loginstate',function(msg){ console.log(msg) self.msg=msg }
) }}</script>
問題解答
回答1:已解決,我原來的文件目錄把eventBus放到了src/components,且文件名是.vue,后來直接放到assets/下面文件名改為.js后得到了解決
