Lizard Derad
Lizard Derad

Reputation: 379

how to pass variables from server to app.js? Laravel + Vue

the plugin I want to use let's define variables in plugin declaration within app.js

Vue.use(AirbnbStyleDatepicker, {monthNames: [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'Julyyy',
        'Augusttt',
        'September',
        'October',
        'November',
        'December',
    ],});

however my website has few locales which is managed in server, I usually encode vars from laravel and pass them as props. In this case this does not work. I didn't find how to initialize plugin with options in component, so what is the way to solve this?

Upvotes: 0

Views: 371

Answers (2)

James
James

Reputation: 214

you can define that javascript array before you load "app.js" in the the view. And then just put it in app.js as const.

in view before app.js is inserted:

<script type="text/javascript"> 
  const trans_month = {{getTransMonths()}}
</script>

then in you app.js

Vue.use(AirbnbStyleDatepicker, {monthNames: trans_month,});

or you could use ajax call to server, which will return array of translated months

Upvotes: 1

Lizard Derad
Lizard Derad

Reputation: 379

Just found one solution, but maybe threre's a better one?

  1. Install this package https://packagist.org/packages/laracasts/utilities
  2. In server do
JavaScript::put(['langs' => [
   july => __('configname.july'),
   august => __('configname.august')
]])
  1. in client do this
Vue.use(AirbnbStyleDatepicker, {monthNames: [
       'January',
       'February',
       'March',
       'April',
       'May',
       'June',
       window.langs['july'],
       window.langs['august'],
       'September',
       'October',
       'November',
       'December',
   ],});

Upvotes: 0

Related Questions