, window " <head> , , .
TL;DR
.env
GEODATA_URL="https://geo.some-domain.com"
/geodata.php
<?php
return [
'url' => env('GEODATA_URL')
];
///root.blade.php
<head>
<script>
window.geodataUrl = "{{ config('geodata.url') }}";
</script>
</head>
/JS// /geodataUrl.js
const geodataUrl = {
data() {
return {
geodataUrl: window.geodataUrl,
};
},
};
export default geodataUrl;
<template>
<div>
<a :href="geodataUrl">YOLO</a>
</div>
</template>
<script>
import geodataUrl from '../mixins/geodataUrl';
export default {
name: 'v-foo',
mixins: [geodataUrl],
data() {
return {};
},
computed: {},
methods: {},
};
</script>
TL;DR
, , app.js app.js:
Vue.mixin({
data() {
return {
geodataUrl: window.geodataUrl,
};
},
});
- , window ".
, , . grep, "window.geodataUrl" , , , .
, .
, JavaScript::put([]), , , , , , , , , .
, Vue, window.chartData, JavaScript::put([ 'chartData' => $user->chartStuff ]). chartData , , , PHP window.chartData, , JavaScript::put() .
:
"JavaScript::put" . , "" 6 , .
Vue:
props: {
chartData: {
type: Array,
required: true,
},
},
, JavaScript::put(), Vue , . Vue , Object , . GET / .
, Laravel Vue, / .
, , ES6:
/web.php
Route::get('/charts/{user}/coolchart', 'UserController@getChart')->name('user.chart');
/Http//UserController.php
public function getChart(Request $request, User $user)
{
$data = $user->chart;
return response()->json([
'chartData' => $data,
]);
}
Vue, :
created() {
this.handleGetChart();
},
methods: {
async handleGetChart() {
try {
this.state = LOADING;
const { data } = await axios.get('/charts/${this.user.id}/coolchart');
if (typeof data !== 'object') {
throw new Error('Unexpected server response. Expected object, got: ${data}');
}
this.chartData = data.chartData;
this.state = DATA_LOADED;
} catch (err) {
this.state = DATA_FAILED;
throw new Error('Problem getting chart data: ${err}');
}
},
},
, Vue , , .
, :
computed: {
isLoading() { return (this.state === LOADING); },
isDataLoaded() { return (this.state === DATA_LOADED); },
isDataFailed() { return (this.state === DATA_FAILED); },
},
, :
<div v-show="isLoading">Loading...</div>
<v-baller-chart v-if="isDataLoaded" :data="chartData"></v-baller-chart>
<button v-show="isDataFailed" type="button" @click="handleGetChart">TRY AGAIN</button>