I've been building a web-app using Vue 2.0 and single file components and ran across an issue that I can't figure out and need to solve.
As it currently stands, my relevant file structure is as follows...
main.js
/*
* main.js
*
* Initial entry point for all Vue and component code
*/
import Vue from 'vue'
import VueResource from 'vue-resource'
import App from './App.vue'
import './sockets.js'
Vue.use(VueResource)
new Vue({
el: '#app',
render: element => element(App)
})
sockets.js
import App from './App.vue'
const socket = io('http://localhost:8181')
socket.on('test', payload => App.$set('test', payload))
/*socket.on('lot_change', payload => {
console.log(payload)
//Logic to find differences and reset
})*/
App.vue
<template>
<div id="app">
{{ test }}
<template v-for="lot in allLots">
<lot
:uuid="lot.uuid"
:closed="lot.closed"
:controllerId="lot.controllerId"
:institution="lot.institution"
:number="lot.lotNumber"
:permits="lot.permits"
:taken-spaces="lot.takenSpaces"
:total-spaces="lot.totalSpaces"></lot>
</template>
</div>
</template>
<script>
import Lot from './components/Lot.vue'
export default {
data() {
return {
allLots: [],
test: {}
}
},
components: {
Lot
},
created() {
this.$http.get('/all').then(res => JSON.parse(res.body)).then(data => {
this.allLots = data.results
})
}
}
</script>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
On a new connection, my express
app emits a 'test'
event to the socket which is received by the client socket perfectly fine, however, what I want to do is that the payload from the back-end socket's emission and assign it to the data of the App
component.
In the callback for the socket.on('test')
, I am currently getting an undefined function
type of error for the App.$set
. I have also tried the global API using Vue.set(App, 'test', payload)
which also didn't work.
Any help would be greatly appreciated!
EDIT
In other words, how can I manipulate the data
object of a single file component from another JS file after importing it?