Vue引入Ant-Design组件库 并且使用JSX编写

 

先使用Vue-cli3脚手架创建项目

yarn global add @vue/cli

 创建项目

vue create antd-demo

安装ant-design 

yarn add ant-design-vue

跑起来

yarn serve

yarn build

开始准备Ant的引入

引入样式表:App.vue就可以了

  import 'ant-design-vue/dist/antd.css'

 按需引入:记得使用Vue.use安装

 import {Button, Icon, Row, Col, Divider} from 'ant-design-vue';

  Vue.use(Button).use(Icon).use(Row).use(Col).use(Divider);

 然后就可以使用JSX了。

 

由于之前一直使用React,最好使的就是JSX语法了,Vue里推崇template模板写法,很不习惯,但是模板语法提供的语法糖:v-指定在JSX里都不可以用了。尤其是v-model ,需要我们自己去实现管理,暂时忽略。

Vue也提供了render函数进行渲染,和React一样,我们想编写JSX,发现Ws里不识别了,因为现在的后缀是xx.Vue不是xx.jsx

可以做这么做:这样就可以在Render的return里写html了。

<script type="text/jsx">

 下面是一个很长的antDemo,使用的是JSx写法,其中自定事件要注意:没有v-on和@ 我们需要遵循驼峰写法,如 @click 在JSx只要写成 onClick 自定义的 close事件 要写成 onClose 一般都是加个on。

而属性也没有v-bind 和: 需要换成 {this.xx},JSX会识别的,有React经验的应该很熟悉,至于循环我使用的是map函数遍历。

而if可以使用三元运算来解决。

<script type="text/jsx">
    import Vue from 'vue'
    import {Button, Icon, Row, Col, Divider, Affix, Dropdown, Menu, Drawer, Modal, Popconfirm, Spin, BackTop

    ,Table
    }
    from
    'ant-design-vue'
    import ACol from "ant-design-vue/es/grid/Col";
    import ARow from "ant-design-vue/es/grid/Row";

    Vue.use(Button).use(Icon).use(Row).use(Col).use(Divider).use(Affix).use(Dropdown).use(Menu).use(Drawer).use(Modal).use(Popconfirm).use(Spin)
        .use(BackTop).use(Table);
    export default {
        name: "demo",
        render() {
            return (
                <div class="scroll-box">
                    <a-divider>按钮使用</a-divider>
                    <a-button type="primary">Button</a-button>
                    <a-divider>图标使用</a-divider>
                    <a-icon type="dribbble" style="color:red" spin/>

                    <a-divider>栅格布局</a-divider>
                    <a-row>
                        <a-col xs={4} md={8}>这是第1个</a-col>
                        <a-col xs={4} md={8}>这是第2个</a
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: vue01@0.1.0 npm ERR! Found: vue@2.7.14 npm ERR! node_modules/vue npm ERR! peerOptional vue@"^2 || ^3.2.13" from @vue/babel-preset-app@5.0.8 npm ERR! node_modules/@vue/babel-preset-app npm ERR! @vue/babel-preset-app@"^5.0.8" from @vue/cli-plugin-babel@5.0.8 npm ERR! node_modules/@vue/cli-plugin-babel npm ERR! dev @vue/cli-plugin-babel@"~5.0.0" from the root project npm ERR! peerOptional vue@"*" from @vue/babel-preset-jsx@1.4.0 npm ERR! node_modules/@vue/babel-preset-jsx npm ERR! @vue/babel-preset-jsx@"^1.1.2" from @vue/babel-preset-app@5.0.8 npm ERR! node_modules/@vue/babel-preset-app npm ERR! @vue/babel-preset-app@"^5.0.8" from @vue/cli-plugin-babel@5.0.8 npm ERR! node_modules/@vue/cli-plugin-babel npm ERR! dev @vue/cli-plugin-babel@"~5.0.0" from the root project npm ERR! 3 more (vue-baidu-map, vuex, the root project) npm ERR! npm ERR! Could not resolve dependency: npm ERR! @ant-design/icons-vue@"^6.1.0" from the root project npm ERR! npm ERR! Conflicting peer dependency: vue@3.0.3 npm ERR! node_modules/vue npm ERR! peer vue@">=3.0.3" from @ant-design/icons-vue@6.1.0 npm ERR! node_modules/@ant-design/icons-vue npm ERR! @ant-design/icons-vue@"^6.1.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! E:\nodejs\node_cache\_logs\2023-06-12T01_03_10_463Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: E:\nodejs\node_cache\_logs\2023-06-12T01_03_10_463Z-debug-0.log
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值