在三元运算vuejs中返回多个值

yxyvkwin  于 2023-03-31  发布在  Vue.js
关注(0)|答案(1)|浏览(206)

我在vuejs中内置了一个dapp,它具有以下功能:

export function useBalanceRefetch() {
    const userStore = useUserStore();
    
    const fetchBalance = (address: string, currency: string) => {
        const fetcher = currency === 'eth' ? fetchEthBalance : fetchArbBalance;

        fetcher(address).then(balance => {
            if (balance) {
                userStore[currency === 'eth' ? 'balanceEth' : 'balanceArb'] = balance;
            }
        });
    };

    return { fetchBalance };
}

一切正常,但我需要添加另一个令牌。
我尝试了以下方法:

export function useBalanceRefetch() {
    const userStore = useUserStore();
    
    const fetchBalance = (address: string, currency: string) => {
        const fetcher = currency === 'eth' ? fetchEthBalance ? fetchUsdtBalance : fetchArbBalance;

        fetcher(address).then(balance => {
            if (balance) {
                userStore[currency === 'eth' ? 'balanceEth' ? 'balanceUsdt' : 'balanceArb'] = balance;
            }
        });
    };

    return { fetchBalance };
}

但它给了我一个错误:

const fetcher = currency === 'eth' ? fetchEthBalance ? fetchUsdtBalance : fetchArbBalance;

以及:

userStore[currency === 'eth' ? 'balanceEth' ? 'balanceUsdt' : 'balanceArb'] = balance;

任何帮助将是伟大的...我试图找出它没有成功。

sy5wg1nm

sy5wg1nm1#

三元意味着涉及三个部分

  • 条件 * ?value_if_true:value_if_false

你可以绕过嵌套更多三元表达式的障碍,但我推荐用有限的方式

userStore[currency === 'eth' ? 'balanceEth' : (currency === 'usd' ? 'balanceUsdt' : 'balanceArb')] = balance;

相关问题