最近根據(jù)設(shè)計(jì)要求app需要根據(jù)不同環(huán)境切換不同樣式,網(wǎng)上找了很多方法都不理想,后面自己腦洞大開(kāi)這么完成的,請(qǐng)大佬多指教!
一、新建全局變量js文件和公用樣式文件,在main.js中引入
import?'./css/en.css'
import?'./css/a.css'
import?globalVariable?from?'./components/js/global_variable'
Vue.prototype.GLOBA?=?globalVariable
二、global_variable.js文件中這么寫(xiě)
let?lang?=?JSON.parse(localStorage.getItem('language_s')?==?null???1?:?localStorage.getItem('language_s'))
let?en?=?""
if(lang?===?1){
????en?=?""
}else{
????en?=?"a"
}
export?default?{
?????mar_l:"mar_l"?+?en,
}?
三、html中引入類(lèi)名
:class="GLOBA.mar_l"
四、切換點(diǎn)擊事件
用localStorage保存值后刷新瀏覽器。
注意:ios需要用?location.reload()