I'm trying to transfer data form my main vue js to a vue component. My project is built with laravel. I've app.js that mainly work for vue. And also have other components. I would like to transfer data between them.
Please check my code.
This is my app.js
require('./bootstrap');
import Vue from "vue";
import VueResource from 'vue-resource'
Vue.use(VueResource);
Vue.http.headers.common['X-CSRF-TOKEN'] = $('input[name="csrf-token"]').attr('value');
Vue.component('google-map', require('./components/GoogleMap.vue'));
const app = new Vue({
el: '#app',
data: {
lat: 0,
lng: 0,
center: { lat: 45.508, lng: -73.587 },
},
created(){
this.lat = 34.20;
this.lng = 100.36;
this.setCenter();
},
methods: {
setCenter(){
this.center.lat = parseFloat(this.lat);
this.center.lng = parseFloat(this.lng);
}
}
});
This is my GoogleMap.vue
<template>
<div>
</div>
</template>
<script>
export default {
name: "GoogleMap",
data() {
return {
center: { lat: 45.508, lng: -73.587 },
};
},
mounted() {
this.centerFun();
},
methods: {
centerFun(){
console.log(this.center);
}
}
};
</script>
This is my map.php
<html>
<head>
<title>Map</title>
</head>
<body>
<div id="app">
<label>Lat</label><input type="text" name="" v-model="lat" @keyup="testFun">
<label>Lng</label><input type="text" name="" v-model="lng" @keyup="testFun">
<google-map></google-map>
</div>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
You can see there are two "center" var.
Can I bind those to a single one? Or what can I do?
If I've multiple components, how can I transfer data form app.js to those components or those components to app.js?