vue.js3:在css中使用变量(vue@3.2.6)


一,js代码

1,css代码:定义变量
2,html代码:调用变量 直接用var函数即可:
<tabBar id="tabbar" style="width:100%;font-size:0.20rem;background:var(--tabbar-background)">
        <tabBarItem path="/home/home" activeColor="#CE1F6F">
          <template v-slot:item-icon=''>
            <img :src="tabList.one" alt />
          template>
          <template v-slot:item-icon-active=''>
            <img :src="tabList.one" alt />
          template>
          <template v-slot:item-text=''>
            <div>首页div>
          template>
        tabBarItem>
        <tabBarItem path="/goods/list" activeColor="#CE1F6F">
          <template v-slot:item-icon=''>
            <img :src="tabList.two" alt />
          template>
          <template v-slot:item-icon-active=''>
            <img :src="tabList.two" alt />
          template>
          <template v-slot:item-text=''>
            <div>分类div>
          template>
        tabBarItem>
        <tabBarItem path="/cart/cart" activeColor="#CE1F6F">
          <template v-slot:item-icon=''>
            <img :src="tabList.three" alt />
          template>
          <template v-slot:item-icon-active=''>
            <img :src="tabList.three" alt />
          template>
          <template v-slot:item-text=''>
            <div>购物车div>
          template>
        tabBarItem>
        <tabBarItem path="/user/info" activeColor="#CE1F6F">
 
          <template v-slot:item-icon=''>
            <img :src="tabList.four" alt />
          template>
          <template v-slot:item-icon-active=''>
            <img :src="tabList.four" alt />
          template>
          <template v-slot:item-text=''>
            <div>个人中心div>
          template>
        tabBarItem>
      tabBar>
  3,js代码:修改变量:
          //设置tabbar的背景颜色值:
          //document.body.style.setProperty('--tabbar-background', '#af2c36');
          document.body.style.setProperty('--tabbar-background', res.data.setting.tabBarColor);

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,测试效果

1,未设置变量时的默认背景色:red   2,设置变量后,背景色会改变为指定颜色:

三,查看vue.js的版本:

liuhongdi@lhdpc:/data/vue/storeweb$ npm list vue
storeweb@0.1.0 /data/vue/storeweb
├─┬ @vue/cli-plugin-babel@4.5.13
│ └─┬ @vue/babel-preset-app@4.5.13
│   └── vue@3.2.6 deduped
├─┬ element-plus@1.1.0-beta.7
│ └── vue@3.2.6 deduped
├─┬ vue-router@4.0.11
│ └── vue@3.2.6 deduped
├── vue@3.2.6
└─┬ vue3-carousel@0.1.27
  └── vue@3.2.6 deduped