Issue while trying to pass json of translation key-value from laravel blade to vue.js

北战南征 提交于 2019-12-10 15:32:42

问题


This is my translation file

return [
    "Key1" =>  "Message 1",
    "Key2" => "Message 2",
    "Key3" => "Message 3",
    "Key4" => "Message 4",
    "Key5" => "Message 5",
    "Key6" => "Message 6",
    "Key7" => "Message 7",
    "Key8" => "Message 8",
];

This is the code in Laravel Blade

<profile
    v-bind:ErrorMessages= "{                            
    Messages: '{!! json_encode(Lang::get('Profile')) !!}'
}">                                
</profile>

In the above component, I am trying to pass the complete translation file from laravel blade to Vue.js

But, the above code print all keys in the web page and disturbs the whole layout.

Am I missing anything to pass the json in correct format from laravel to Vue.js

Update 1

I am able to pass the object from laravel to vue.js using below code. But below is more like manual work typing each translation key of a file one by one.

<profile
    v-bind:messages= "{                            
    Key1: '{!! trans('Profile.Key1') !!}',
    Key2: '{!! trans('Profile.Key2') !!}',
    Key3: '{!! trans('Profile.Key3') !!}',
    Key4: '{!! trans('Profile.Key4') !!}',
    Key5: '{!! trans('Profile.Key5') !!}',
    Key6: '{!! trans('Profile.Key6') !!}',
    Key7: '{!! trans('Profile.Key7') !!}',
    Key8: '{!! trans('Profile.Key8') !!}'
}">                                
</profile>

回答1:


Try this:

<profile
    v-bind:ErrorMessages= "'{!! json_encode(Lang::get('Profile')) !!}'">                                
</profile>

Note about additional ' between " and { - so you'll pass what you want as string.




回答2:


It seems that you need to pass an object to :messages

I'm not very familiar with Laravel, please try following option:

<profile messages='{!! json_encode(Lang::get("Profile")) !!}'></profile>

or

<profile :messages="{!! json_encode(Lang::get('Profile')) !!}"></profile>


来源:https://stackoverflow.com/questions/55778163/issue-while-trying-to-pass-json-of-translation-key-value-from-laravel-blade-to-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!