如何使用另一个字符串替换字符串(Vue3 组合 API)

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

How to replace a string with another string (vue3 composition API)

问题

I have strings that contain ### and I'm replacing it with another value. Now I want to use com in combined with a click event, I created the event and it works but it doesn't go back to the original value

What's the best way to do this?

<script setup>
const isOpen = ref(false)

const changeString = ref('Exibir Filtros')

function toggle() {
    isOpen.value = !isOpen.value
    changeString.value = 'Ocultar Filtros'
}
</script>

As soon as the user clicks on toggle, a dropdown appears and the value of the div should change to "Ocultar Filtros" and when the user clicks again the value of the div should return to the previous value "Exibir Filtros"

<div id="faq-accordion-2" class="accordion accordion-boxed">
    <div class="accordion-item">
        <div id="faq-accordion-content-5" class="accordion-header flex items-center">
            <div class="w-50vw relative text-slate-500">
                <input type="text" class="form-control box pr-10" id="search" placeholder="Busque por detalhes, número, nome, vencimento ou valor">
                <Search class="w-4 h-4 absolute my-auto inset-y-0 mr-3 right-0"></Search>
            </div>
            <div class="underline ml-4 text-primary font-medium cursor-pointer" @click="toggle">{{ changeString }}</div>
        </div>
        <div id="faq-accordion-collapse-5" class="accordion-collapse" v-show="isOpen">
            <div class="accordion-body text-slate-600 dark:text-slate-500 leading-relaxed">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</div>
        </div>
    </div>
</div>
英文:

I have strings that contain ### and I'm replacing it with another value. Now I want to use com in combined with a click event, I created the event and it works but it doesn't go back to the original value

What's the best way to do this?

&lt;script setup&gt;
const isOpen = ref(false)

const changeString = ref(&#39;Exibir Filtros&#39;)


function toggle() {
    isOpen.value = !isOpen.value
    changeString.value = &#39;Ocultar Filtros&#39;
}


&lt;/script&gt;

As soon as the user clicks on toggle, a dropdown appears and the value of the div should change to "Ocultar Filtros" and when the user clicks again the value of the div should return to the previous value "Exibir Filtros"

&lt;div id=&quot;faq-accordion-2&quot; class=&quot;accordion accordion-boxed&quot;&gt;
                    &lt;div class=&quot;accordion-item&quot;&gt;
                        &lt;div id=&quot;faq-accordion-content-5&quot; class=&quot;accordion-header flex items-center&quot;&gt;
                            &lt;div class=&quot;w-50vw relative text-slate-500&quot;&gt;
                                &lt;input type=&quot;text&quot; class=&quot;form-control box pr-10&quot; id=&quot;search&quot;
                                    placeholder=&quot;Busque por detalhes, n&#250;mero, nome, vencimento ou valor&quot;&gt;
                                &lt;Search class=&quot;w-4 h-4 absolute my-auto inset-y-0 mr-3 right-0&quot; /&gt;
                            &lt;/div&gt;
                            &lt;div class=&quot;underline ml-4 text-primary font-medium cursor-pointer&quot; @click=&quot;toggle&quot;&gt;{{ changeString }}&lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div id=&quot;faq-accordion-collapse-5&quot; class=&quot;accordion-collapse&quot; v-show=&quot;isOpen&quot;&gt;
                            &lt;div class=&quot;accordion-body text-slate-600 dark:text-slate-500 leading-relaxed&quot;&gt; Lorem Ipsum is
                                simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the
                                industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley
                                of type and scrambled it to make a type specimen book. It has survived not only five
                                centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;

答案1

得分: 1

  1. 使用三元操作符的方式:
function toggle() {
    isOpen.value = !isOpen.value
    changeString.value = isOpen.value ? 'Ocultar Filtros' : 'Exibir Filtros'
}
  1. 你还可以在 computed() 属性中使用这个三元操作符,根据 isOpen 的变化来计算 changeString
const changeString = computed(() => isOpen.value ? 'Ocultar Filtros' : 'Exibir Filtros');
英文:

There are two other ways you can achieve this:

  1. Using tenary operation
function toggle() {
    isOpen.value = !isOpen.value
    changeString.value = isOpen.value ? &#39;Ocultar Filtros&#39; : &#39;Exibir Filtros&#39;
}
  1. You can also use this tenary operation in computed() property where you compute changeString based on change in isOpen
const changeString = computed(() =&gt; isOpen.value ? &#39;Ocultar Filtros&#39; : &#39;Exibir Filtros&#39;
)

答案2

得分: 0

It sounds like changeString should set itself based on the value of isOpen

function toggle() {
    isOpen.value = !isOpen.value
    if (isOpen.value) {
       changeString.value = 'Ocultar Filtros'
    } else {
       changeString.value = 'Exibir Filtros'
    }
}
英文:

It sounds like changeString should set itself based on the value of isOpen

function toggle() {
    isOpen.value = !isOpen.value
    if (isOpen.value) {
       changeString.value = &#39;Ocultar Filtros&#39;
    } else {
       changeString.value = &#39;Exibir Filtros&#39;
    }
}

huangapple
  • 本文由 发表于 2023年3月1日 08:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598550.html
匿名

发表评论

匿名网友

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

确定