情况如下:
我有多个按钮来执行不同的异步功能。
都是进度按钮
我想要的是,当按下按钮时,标签会更改,指示功能的状态(功能名称、加载、完成或错误,如果需要的话)
一切正常,但文字没有改变
注意:目前我还没有实现所有四个功能,但一个应该可以工作
这是组件。
组件 1 - 容器
<script>
import Button from '../components/Button.svelte'
import { cubiqSetUp } from '../../../store/store.js'
import { fly } from 'svelte/transition'
import URLS from '../../../api/endpoints.js'
let loadingEffect = false
let ButtonText = null
async function calibrate(current) {
console.log(current.detail)
loadingEffect = true
ButtonText = 'CARGANDO...'
console.log(ButtonText)
let { calibrateURL } = await URLS()
await fetch(calibrateURL)
.then(response => {
loadingEffect = false
ButtonText = 'LISTO'
response.json()
setTimeout(() => {
ButtonText = current.detail
}, 1500)
})
.catch(error => {
ButtonText = 'ERROR'
loadingEffect = false
setTimeout(() => {
ButtonText = current.detail
}, 1500)
})
}
</script>
<div class="Buttons">
<div class="Buttons-container">
<Button
on:active={() => console.log('Pending')}
{loadingEffect}
ButtonText="CUBICAR" />
<Button
on:active={calibrate}
{loadingEffect}
ButtonText="CALIBRAR"
delay={400} />
{#if $cubiqSetUp.print_info === 'true' && $cubiqSetUp.OCR === 'true'}
<Button ButtonText="IMPRIMIR" delay={800} />
{:else if $cubiqSetUp.print_info === 'true'}
<Button ButtonText="GUARDAR" delay={800} />
<Button ButtonText="IMPRIMIR" delay={1200} />
{:else if $cubiqSetUp.OCR === 'true'}
<!-- No additional buttons -->
{:else}
<Button ButtonText="GUARDAR" delay={800} />
{/if}
</div>
</div>
组件 1 - 按钮
<script>
import { fly } from 'svelte/transition'
import { createEventDispatcher } from 'svelte'
const dispatch = createEventDispatcher()
export let ButtonText = ''
export let delay = 200
export let loadingEffect = false
</script>
<div
transition:fly={{ delay, y: 200, duration: 2000 }}
on:click={() => dispatch('active', ButtonText)}
class="progress-btn {loadingEffect ? 'active' : ''}">
<div class="btn">{ButtonText}</div>
<div class="progress" />
</div>
如果你们给我看一下,我会很感激