這是我在main.js
如何從Vue js的App.vue獲取Main.js中的數據?
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import vueResource from 'vue-resource'
// import {bus } from './bus.js'
// import MainContent from './components/MainContent'
export const bus = new Vue();
Vue.config.productionTip = true
Vue.use(vueResource)
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
// {path:'/',component: MainContent }
]
})
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
// const bus = require('./bus.js');
var newData = new Vue({
el: '#graph-content',
data: {
graphD:'',
title: 'test title'
},
methods: {
fetchData: function (data) {
this.graphD = data;
console.log('Inside', this.graphD);
}
},
created: function() {
bus.$on('graphdata', this.fetchData);
}
})
console.log('OutSide', newData._data.graphD)
代碼這裏是公交車從app.vue
bus.$emit('graphdata', "test bus");
發出的數據使用下面的代碼我試圖從應用程序獲取數據。 VUE。我正在獲取fetchData範圍內的數據,但我沒有從newData範圍之外獲取數據。 任何人都可以建議我問題或可能的解決方案將數據/對象從app.vue
傳遞到main.js
文件。
****更新***** app.vue
<template>
<div>
<app-header> </app-header>
<app-maincontent></app-maincontent>
</div>
</template>
<script>
import Header from './components/Header.vue'
import SideNav from './components/SideNav.vue'
import MainContent from './components/MainContent.vue'
import GraphA from './components/GraphA.vue'
import {bus} from './main.js'
var apiURL = 'http://localhost:3000/db';
export default {
components: {
'app-header': Header,
'app-sidenav': SideNav,
'app-maincontent': MainContent,
'app-grapha': GraphA
},
data() {
return {
users:[]
}
},
methods: {
fetchData: function() {
var self = this;
$.get(apiURL, function(data) {
self.users = data;
// console.log(data);
bus.$emit('graphdata', "test bus");
});
}
},
created: function(){
this.fetchData();
}
}
</script>
<style>
</style>
添加您的全app.vue文件代碼。你是否創建了vue的兩個實例,並保持一個總線實例獨立的vue實例。你如何實現'bus'? –
我更新了文件。你能檢查出來嗎! 謝謝 – pskkar