简体   繁体   中英

Laravel blade and Vue.js, concatenate php function and vue.js javascript variable inside a template

I would like to know how I can concatenate laravel php function lang with javascript variable. Here an example:

This code not work. The variable text is on javascript an is dynamic. The php function lang does not translate the concatenated word, display reportParameter.txt_date

Not work display reportParameter.txt_date

template: `
<div class="text-center mt-1">
    <div class="custom-control custom-radio custom-control-inline" v-for="(text, val, index) in optionsParsed">
        <input class="custom-control-input" type="radio" v-model="checked" :id="nomRadio+'_'+index" :name="nomRadio" :value="val" @change="getChecked(checked)">
        <label class="custom-control-label" :for="nomRadio+'_'+index">@lang('reportParameter.'.@{{text}}')</label>
    </div>
</div>

`

it works display Date

 template: `
<div class="text-center mt-1">
    <div class="custom-control custom-radio custom-control-inline" v-for="(text, val, index) in optionsParsed">
        <input class="custom-control-input" type="radio" v-model="checked" :id="nomRadio+'_'+index" :name="nomRadio" :value="val" @change="getChecked(checked)">
        <label class="custom-control-label" :for="nomRadio+'_'+index">@lang('reportParameter.text_date')</label>
    </div>
</div>`

The problem is the variable text must be generated dynamically. The problem is only how I do the concatenation. I don't concatenate there correctly. I try a lot of different ways, but no success.

Thank you so much for your help.

You can simply add a placeholder to your HTML tag like this:

<template>
  <p>Hallo my name is {{ name }}</p>
</template>

Then if you want to get the name dynamically from the server you simply post a request to get the name.

You can for instance use axios for that.

<script>
export default {
  data: () => ({
    name: ''
  }),

  // going to run if the app gets mounted / page reload so to say
  mounted() {
    getName();
  },

  methods: {
    getName() {
      axios.get('/api/name')
           .then(response => name = response.data)
           .catch(error => console.log(error)
    }
  }
}
</script>

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