简体   繁体   English

如何将变量从服务器传递到 app.js? Laravel + Vue

[英]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我想使用的插件让我们在 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.但是我的网站几乎没有在服务器中管理的语言环境,我通常对 laravel 中的变量进行编码并将它们作为道具传递。 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.您可以在视图中加载“app.js”之前定义 javascript 数组。 And then just put it in app.js as const.然后将其作为 const 放入 app.js 中。

in view before app.js is inserted:在插入 app.js 之前查看:

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

then in you app.js然后在你 app.js

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

or you could use ajax call to server, which will return array of translated months或者您可以使用 ajax 调用服务器,这将返回翻译月份的数组

Just found one solution, but maybe threre's a better one?刚刚找到了一种解决方案,但也许还有更好的解决方案?

  1. Install this package https://packagist.org/packages/laracasts/utilities安装此 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',
   ],});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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