首页 > 其他分享 >vue中使用jsx

vue中使用jsx

时间:2023-04-14 16:55:21浏览次数:52  
标签:vue const return item 使用 import jsx

前言

相对来说 有些时候用jsx更合适,更灵活些

安装依赖

有对应的包支持

yarn add --dev @vitejs/plugin-vue-jsx

配置插件

在vite.config.js

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx({})
});

使用

ant的左侧导航,vue的模板语法写法就体验很糟糕,这里我将其改为jsx模式。

import { computed, defineComponent, resolveComponent, h  } from "vue";
import { useStore } from "/@/store";
import { useRoute } from "vue-router";
import logoImg from "/@/assets/logo.png";
import styled from "@magister_zito/vue3-styled-components";
import router from "/@/router";

const StyledLogo = styled.div`
  height: 32px;
  margin: 16px;
  background: rgba(255, 255, 255, 0.3);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 18px;
  img {
    height: 32px;
    margin-right: 10px;
  }
`;

const toMenu = (menu: any) => {
  router.push(menu.path);
};

const SubMenu = defineComponent({
  props: {
    menuInfo: {
      type: Object,
      default: "请输入",
    },
  },
  setup(props) {
    return () => (
      <a-sub-menu
        key={props.menuInfo.path}
        v-slots={{
          title: () => <span>{props.menuInfo.title}</span>,
          icon: () =>  h(resolveComponent(props.menuInfo.meta.icon))
        }}
      >
        {props.menuInfo.children.map((item: any) =>
          item.children ? (
            <SubMenu menu-info={item} key={item.path} />
          ) : (
            <a-menu-item
              key={item.path}
              onClick={() => {
                toMenu(item);
              }}
            >
              {item.title}
            </a-menu-item>
          )
        )}
      </a-sub-menu>
    );
  },
});



export default defineComponent({
  name:'LayoutSider',
  setup() {
    const route = useRoute();
    const store = useStore();

    // 当前高亮的选中菜单
    const selectedKeys = computed<string[]>(() => {
      return [route.path];
    });

    // 处理刷新后 显示的二级菜单或者三级菜单
    const openKeys = computed<string[]>(() => {
      let arr = [];
      arr = route.path.split("/").splice(1, route.path.split("/").length);
      arr = arr.map((item) => {
        item = "/" + item;
        return item;
      });
      return arr;
    });

    return () => (
      <a-layout-sider collapsed={store.openMenu}>
        <StyledLogo>
          <img src={logoImg} />
          { !store.openMenu && <span>Ant Design</span>}
        </StyledLogo>
        <a-menu
          style="width: auto !important"
          v-model:openKeys={openKeys.value}
          v-model:selectedKeys={selectedKeys.value}
          mode="inline"
          theme="dark"
        >
          {store.siteInfo.menus.map((item: any) =>
            item.children ? (
              <SubMenu key={item.path} menuInfo={item} />
            ) : (
              <a-menu-item
                key={item.path}
                onClick={() => {
                  toMenu(item);
                }}
                v-slots={{
                  icon: () =>  h(resolveComponent(item.meta.icon))
                }}
              >
                {item.title}
              </a-menu-item>
            )
          )}
        </a-menu>
      </a-layout-sider>
    );
  },
});

其它

如上 也看到了 如果不像样式分离,我也可以使用 styled 方案,即css in js

标签:vue,const,return,item,使用,import,jsx
From: https://www.cnblogs.com/dingshaohua/p/17318826.html

相关文章

  • Spring Boot 中使用 Redis
    Redis环境redis安装、配置,启动:(此处以云服务器上进行说明)下载地址:https://redis.io/download/下载后上传到云服务器上,如/usr/local中gcc环境安装:yuminstall-ygcc-c++解压:tar-zxvfxxx进入解压后的redis目录下执行编译:make安装:makeinstall下载redis/bin/r......
  • nginx使用
    我使用的是windows环境1、下载nginx包文件https://nginx.org/en/download.html2、解压3、运行nginx.exe4、输入localhost:80启动成功 自己的项目在nginx中运行1、本地dist文件内容粘贴到nginx文件下的html目录中2、更改配置添加重定向,避免路由跳转404配置代理 ......
  • redis里使用lua脚本对hash里的数据进行排序
    注意:本脚本只适用于数据量较少的集合进行排序,数据量太大会严重影响redis的性能。localt=redis.call('hgetall',KEYS[1]);localarr={};fori,vinpairs(t)doifi%2==0thenlocalj=cjson.decode(v)ifj.language==ARGV[1]thenj.lan......
  • Linux常用命令-个人使用
    Linux常用命令-个人使用本人在centos下常用的命令,其他系统机器不确定是否可以使用1.nmtui命令启动界面改IP信息首先要启动NetworkManager服务,然后运行nmtui命令就可以手动配置网卡信息,贼好用运行nmtui命令2.ls根据文件大小排序ll-hS|grep^-只显示文件且按文件大小......
  • vue列表渲染之for循环
    vue列表渲染之for循环前端开发中,如果涉及列表渲染,都会提示或要求每个列表项使用唯一的key,那很多开发者就会直接使用数组的index作为key的值,而并不知道key的原理。那么以下会讲解key的作用以及为什么最好不要使用index作为key的属性值。1、作用在虚拟DOM中,key是虚......
  • Mybatis-plus 中Wrapper的使用
    mybatisplus条件构造器关系图1.上图绿色框为抽象类abstract2.蓝色框为正常class类,可new对象3.黄色箭头指向为父子类关系,箭头指向为父类QueryWrapper继承自AbstractWrapper,自身的内部属性entity也用于生成where条件及LambdaQueryWrapper,可以通过newQueryWrap......
  • 功能不够用?使用C++编写通达信插件及接入Python(一)
    第一次尝试,参照:http://www.xiaoyunyun.net/index.php/archives/53.html 和 https://blog.csdn.net/wiowei/article/details/121466094在绑定DLL环节失败了第二次尝试:参照:https://zhuanlan.zhihu.com/p/5698198681.修改VS2019,勾选 windows10SDK2.用Visualstudio打开......
  • JavaScript 使用 reduce 方法实现简单的 i18n 功能
    JavaScript使用reduce方法实现简单的i18n功能i18n:国际化(Internationalization)的缩写使用Array.prototype.reduce()方法实现简单的i18n功能reduce()方法对数组中的每个元素按序执行一个由您提供的reducer函数,每一次运行reducer会将先前元素的计算结果作为参......
  • vue通过Export2Excel.js进行导入excel,获取数据
    <!--封装的模板下载和导入按钮和功能组件--><template><spanstyle="margin-left:10px"><el-buttonsize="mini"class="el-icon-download"@click="downFiles">下载模板</el-button><el-upload......
  • python 正则处理字符串,使用函数
    """在正则截取的字符子串基础上,处理字符串Python的re模块提供了re.sub用于替换字符串中的匹配项。语法:re.sub(pattern,repl,string,count=0,flags=0)参数:pattern:正则中的模式字符串。repl:替换的字符串,也可为一个函数。string:要被查找替换的原始字符串。cou......