将动态文本值传递给Vue.js中的共享按钮组件

huangapple go评论88阅读模式
英文:

Passing dynamic text value to shared button component in Vuejs

问题

以下是您要翻译的内容:

"I'm trying to pass dynamic text value to my shared component named 'Button component' whose text should appear in Spanish or English based on the language chosen.

For translation, I already have translation.service.js doing the conversions.
I'm not sure how to pass the value to the :text parameter below by using the service as the current format for :text is showing error. It's working fine elsewhere in p tags.
Any help/suggestion would be appreciated as I'm not able to move forward.

<Button 
          @on-click="currentStep = 1"
          :text= {{ this.getTranslation("intro_modal_persona_select_button_next") }}  
          bgColor="transparent"
          txtColor="#d64ba1"
/>

The full code in <template> is:

<template>
  <div class="d-flex mb-3 justify-content-between">
    <div class="p-2">
      <Logo :type="logoType" />
    </div>
    <div class="p-2">
      <ButtonIcon symbol="corner-down-left" :disabled="disabled" />
    </div>
  </div>

  <div class="d-flex flex-column">
    <!--Step 1: Language Selection-->

    <p class="step">{{ currentStep }} OF 2</p>
    <p class="community-name">
      {{  community.c_community_name}}
    </p>

    <div class="p-2 flex-fill" v-if="currentStep == 1">

      <VueScrollPicker 
        id="language-scroller"
        :options="options" 
        v-model="selectedLanguage"
      />

      <!--Language selection scroll-->
      <p class="option-select">
        {{ this.getTranslation("intro_modal_persona_select_language") }}
      </p>

      <div 
        v-if="selectedLanguage"
        class="button_container text-center"
      >
        <Button
          @on-click="currentStep = 2"
          :text= {{ this.getTranslation("intro_modal_persona_select_button_next") }} 
        />
      </div>
    </div>

    <!--Step 2: Agent Selection-->
    <div class="p-2 flex-fill" v-if="currentStep == 2">

      <div id="persona-slider-wrapper" ref="persona_slider_wrapper">
        <PersonalSlider :personas="personas" />
      </div>
      <p class="option-select">
        {{ this.getTranslation("intro_modal_persona_select_agent") }}
      </p>

      <div 
        v-if="selectedLanguage"
        class="button_container text-center"
      >
        <Button 
          @on-click="goToDigitalHumanView"
          :text= {{ this.getTranslation("intro_modal_persona_select_button_begin") }} 
          class="mb-2"
        />

        <Button 
          @on-click="currentStep = 1"
          :text= {{ this.getTranslation("intro_modal_persona_select_button_back") }}   
          bgColor="transparent"
          txtColor="#d64ba1"
        />
      </div>

    </div>
  </div>

  <div class="d-flex justify-content-between">
    <MainFooter />
  </div>
</template>

My translation service:


class TranslationService {
    async loadTranslations() {
        return [
            // Intro Modal Persona Select
            {
                'key': 'intro_modal_persona_select_language',
                'en': 'Select your Language',
                'es': 'Elige tu Idioma'
            },

            {
                'key': 'intro_modal_persona_select_button_next',
                'en': 'Next',
                'es': 'Siguiente'
            },

            {
                'key': 'intro_modal_persona_select_agent',
                'en': "Select your agent",
                'es': 'Seleccione su agente'
            },

            {
                'key': 'intro_modal_persona_select_button_begin',
                'en': 'Begin',
                'es': 'Comenzar'
            },
          ];
    }
}

export default new TranslationService()
英文:

I'm trying to pass dynamic text value to my shared component named "Button component" whose text should appear in Spanish or English based on the language chosen.

For translation, I already have translation.service.js doing the conversions.
I'm not sure how to pass the value to the :text parameter below by using the service as the current format for :text is showing error. It's working fine elsewhere in p tags.
Any help/suggestion would be appreciated as I'm not able to move forward.

&lt;Button 
@on-click=&quot;currentStep = 1&quot;
:text= {{ this.getTranslation(&quot;intro_modal_persona_select_button_next&quot;) }}  
bgColor=&quot;transparent&quot;
txtColor=&quot;#d64ba1&quot;
/&gt;

The full code in <template> is:

&lt;template&gt;
&lt;div class=&quot;d-flex mb-3 justify-content-between&quot;&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;Logo :type=&quot;logoType&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;ButtonIcon symbol=&quot;corner-down-left&quot; :disabled=&quot;disabled&quot; /&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;d-flex flex-column&quot;&gt;
&lt;!--Step 1: Language Selection--&gt;
&lt;p class=&quot;step&quot;&gt;{{ currentStep }} OF 2&lt;/p&gt;
&lt;p class=&quot;community-name&quot;&gt;
{{  community.c_community_name}}
&lt;/p&gt;
&lt;div class=&quot;p-2 flex-fill&quot; v-if=&quot;currentStep == 1&quot;&gt;
&lt;VueScrollPicker 
id=&quot;language-scroller&quot;
:options=&quot;options&quot; 
v-model=&quot;selectedLanguage&quot;
/&gt;
&lt;!--Language selection scroll--&gt;
&lt;p class=&quot;option-select&quot;&gt;
{{ this.getTranslation(&quot;intro_modal_persona_select_language&quot;) }}
&lt;/p&gt;
&lt;div 
v-if=&quot;selectedLanguage&quot;
class=&quot;button_container text-center&quot;
&gt;
&lt;Button
@on-click=&quot;currentStep = 2&quot;
:text= {{ this.getTranslation(&quot;intro_modal_persona_select_button_next&quot;) }} 
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!--Step 2: Agent Selection--&gt;
&lt;div class=&quot;p-2 flex-fill&quot; v-if=&quot;currentStep == 2&quot;&gt;
&lt;div id=&quot;persona-slider-wrapper&quot; ref=&quot;persona_slider_wrapper&quot;&gt;
&lt;PersonalSlider :personas=&quot;personas&quot; /&gt;
&lt;/div&gt;
&lt;p class=&quot;option-select&quot;&gt;
{{ this.getTranslation(&quot;intro_modal_persona_select_agent&quot;) }}
&lt;/p&gt;
&lt;div 
v-if=&quot;selectedLanguage&quot;
class=&quot;button_container text-center&quot;
&gt;
&lt;Button 
@on-click=&quot;goToDigitalHumanView&quot;
:text= {{ this.getTranslation(&quot;intro_modal_persona_select_button_begin&quot;) }} 
class=&quot;mb-2&quot;
/&gt;
&lt;Button 
@on-click=&quot;currentStep = 1&quot;
:text= {{ this.getTranslation(&quot;intro_modal_persona_select_button_back&quot;) }}   
bgColor=&quot;transparent&quot;
txtColor=&quot;#d64ba1&quot;
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;d-flex justify-content-between&quot;&gt;
&lt;MainFooter /&gt;
&lt;/div&gt;
&lt;/template&gt;

My translation service:


class TranslationService {
async loadTranslations() {
return [
// Intro Modal Persona Select
{
&#39;key&#39;: &#39;intro_modal_persona_select_language&#39;,
&#39;en&#39;: &#39;Select your Language&#39;,
&#39;es&#39;: &#39;Elige tu Idioma&#39;
},
{
&#39;key&#39;: &#39;intro_modal_persona_select_button_next&#39;,
&#39;en&#39;: &#39;Next&#39;,
&#39;es&#39;: &#39;Siguiente&#39;
},
{
&#39;key&#39;: &#39;intro_modal_persona_select_agent&#39;,
&#39;en&#39;: &quot;Select your agent&quot;,
&#39;es&#39;: &#39;Seleccione su agente&#39;
},
{
&#39;key&#39;: &#39;intro_modal_persona_select_button_begin&#39;,
&#39;en&#39;: &#39;Begin&#39;,
&#39;es&#39;: &#39;Comenzar&#39;
},
];
}
}
export default new TranslationService()

答案1

得分: 1

将值绑定到属性应该像这样进行:v-bind:attr="value":attr="value",处理事件应该像这样进行:v-on:click="eventHandler"@click="eventHandler":

<Button 
  @click="currentStep = 1"
  :text='getTranslation("intro_modal_persona_select_button_next")' 
  bgColor="transparent"
  txtColor="#d64ba1"
/>
英文:

Binding a value to a prop should be done like v-bind:attr=&quot;value&quot; or :attr=&quot;value&quot;, and handling events should be done in this way v-on:click=&quot;eventHandler&quot; or @click=&quot;eventHandler&quot;:

&lt;Button 
          @click=&quot;currentStep = 1&quot;
          :text=&#39;getTranslation(&quot;intro_modal_persona_select_button_next&quot;)&#39; 
          bgColor=&quot;transparent&quot;
          txtColor=&quot;#d64ba1&quot;
/&gt;

huangapple
  • 本文由 发表于 2023年2月26日 19:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定