我正在研究貨幣轉換器。我在單擊鏈接時呼叫方法,并在方法成功后希望它重新加載。但重新加載根本不起作用。貨幣存盤在 cookie 中,如果我手動重繪 頁面,它也會在導航欄中更新,但我希望它在方法完成后自動重新加載。
這是導航欄中的代碼..
<b-dropdown position="is-bottom-left" aria-role="menu">
<template #trigger>
<a class="navbar-item font-bold" role="button">
{{currency}}
<b-icon icon="menu-down"></b-icon>
</a>
</template>
<b-dropdown-item v-for="(item, index) in currencies" :key="index" has-link aria-role="menuitem">
<a class="no-underline" @click="setCurrency(item.iso, (reload = true))"><span class="text-pink-600 font-bold">{{item.fullname}} ({{item.iso}})</span></a>
</b-dropdown-item>
</b-dropdown>
這是方法。
methods: {
setCurrency(newcurrency) {
this.$cookies.set(`usercurrency`, newcurrency, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
// window.location.href = '/'
}
}
我想window.location.href = '/'在設定 cookie 后使用,但我不能這樣做,因為我在創建的鉤子中使用相同的方法,如下面的代碼來設定基于用戶國家的貨幣,它會說視窗未定義。
created() {
const isCurrency = this.$cookies.get('usercurrency');
const isUserCountry = this.$cookies.get('usercountry');
if (isCurrency === undefined) {
if (isUserCountry === undefined) {
fetch("https://ipinfo.io/json?token=*********").then(
(response) => response.json()
)
.then((jsonResponse) => {
const country = jsonResponse.country
this.$cookies.set(`usercountry`, country, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
var CurrencyParam;
switch (country) {
case "IN":
case "NP":
CurrencyParam = "INR";
break;
case "US":
CurrencyParam = "USD";
break;
case "AU":
CurrencyParam = "AUD";
break;
case "CA":
CurrencyParam = "CAD";
break;
case "GB":
CurrencyParam = "GBP";
break;
case "AE":
CurrencyParam = "AED";
break;
case "RU":
CurrencyParam = "RUB";
break;
case "JP":
CurrencyParam = "JPY";
break;
case "SG":
CurrencyParam = "SGD";
break;
case "FR":
case "FI":
case "DE":
case "GR":
case "HU":
case "IT":
case "LT":
case "MT":
case "NL":
case "NO":
case "PL":
case "PT":
case "RO":
case "RS":
case "ES":
case "SE":
case "CH":
case "UA":
CurrencyParam = "EUR";
break;
default:
CurrencyParam = "USD";
}
this.setCurrency(CurrencyParam)
})
.catch((error) => {
console.log(error)
this.setCurrency("USD")
})
}
}
}
uj5u.com熱心網友回復:
快速解決方案可能是reload作為可選引數傳遞給setCurrency
methods: {
setCurrency(newcurrency, reload) {
this.$cookies.set(`usercurrency`, newcurrency, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
if (reload) window.location.href = '/'
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/370136.html
下一篇:ProductScreen.js:12Uncaught(inpromise)TypeError:cannotreadpropertiesofundefined(reading'params&
