reload=true 在@click 函数中不起作用
reload=true not working in @click function
我正在研究货币转换器。我在单击 link 时调用方法,在方法成功后希望它重新加载。但重新加载根本不起作用。货币存储在 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 = '/'
}
}
我想在设置 cookie 后使用 window.location.href = '/'
但我不能这样做,因为我在创建的挂钩中使用相同的方法,如下面的代码根据用户国家/地区设置货币,它会说 window 未定义。
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')
})
}
}
},
快速解决方案可能是将 reload
作为可选参数传递给 setCurrency
methods: {
setCurrency(newcurrency, reload) {
this.$cookies.set(`usercurrency`, newcurrency, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
if (reload) window.location.href = '/'
}
}
我正在研究货币转换器。我在单击 link 时调用方法,在方法成功后希望它重新加载。但重新加载根本不起作用。货币存储在 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 = '/'
}
}
我想在设置 cookie 后使用 window.location.href = '/'
但我不能这样做,因为我在创建的挂钩中使用相同的方法,如下面的代码根据用户国家/地区设置货币,它会说 window 未定义。
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')
})
}
}
},
快速解决方案可能是将 reload
作为可选参数传递给 setCurrency
methods: {
setCurrency(newcurrency, reload) {
this.$cookies.set(`usercurrency`, newcurrency, {
path: '/',
maxAge: 60 * 60 * 24 * 7
})
if (reload) window.location.href = '/'
}
}