简体   繁体   中英

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?

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

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',
   ],});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM