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

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

1
yarn global add @vue/cli

null

创建项目

1
vue create antd-demo

null

安装ant-design

1
yarn add ant-design-vue

null

跑起来

1
2
3
yarn serve

yarn build

null

开始准备Ant的引入

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

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

null

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

1
2
3
4
import {Button, Icon, Row, Col, Divider} from 'ant-design-vue'

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

null

然后就可以使用JSX了。

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

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

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

1
<script type="text/jsx">

null

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<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-col>
<a-col xs={4} md={8}>这是第3个</a-col>

</a-row>
<a-divider>固钉</a-divider>
<a-affix>固定</a-affix>
<a-divider>下拉菜单</a-divider>
<a-dropdown-button onClick={this.dropDownClick.bind(this)} trigger={['contextmenu']}> 右键显示
<a-menu slot="overlay">
<a-menu-item key="1" onClick={this.dropDownClick.bind(this, 1)}>
<a-icon type="user"/>
1st menu item
</a-menu-item>
<a-menu-item key="2" onClick={this.dropDownClick.bind(this, 2)}>
<a-icon type="user"/>
2nd menu item
</a-menu-item>
<a-menu-item key="3" onClick={this.dropDownClick.bind(this, 3)}>
<a-icon type="user"/>
3rd item
</a-menu-item>
</a-menu>
</a-dropdown-button>
<a-dropdown-button onClick={this.dropDownClick.bind(this)}> 悬浮显示
<a-menu slot="overlay">
<a-menu-item key="1" onClick={this.dropDownClick.bind(this, 1)}>
<a-icon type="user"/>
1st menu item
</a-menu-item>
<a-menu-item key="2" onClick={this.dropDownClick.bind(this, 2)}>
<a-icon type="user"/>
2nd menu item
</a-menu-item>
<a-menu-item key="3" onClick={this.dropDownClick.bind(this, 3)}>
<a-icon type="user"/>
3rd item
</a-menu-item>
</a-menu>
</a-dropdown-button>
<a-dropdown-button onClick={this.dropDownClick.bind(this)} trigger={['click']}> 点击显示
<a-menu slot="overlay">
<a-menu-item key="1" onClick={this.dropDownClick.bind(this, 1)}>
<a-icon type="user"/>
1st menu item
</a-menu-item>
<a-menu-item key="2" onClick={this.dropDownClick.bind(this, 2)}>
<a-icon type="user"/>
2nd menu item
</a-menu-item>
<a-menu-item key="3" onClick={this.dropDownClick.bind(this, 3)}>
<a-icon type="user"/>
3rd item
</a-menu-item>
</a-menu>
</a-dropdown-button>
<a-divider>锚点菜单</a-divider>
<a-dropdown>
<a className="ant-dropdown-link" href="#">
悬浮预览菜单 <a-icon type="down"/>
</a>
<a-menu slot="overlay">
<a-menu-item>
<a href="javascript:;">1st menu item</a>
</a-menu-item>
<a-menu-item>
<a href="javascript:;">2nd menu item</a>
</a-menu-item>
<a-menu-item>
<a href="javascript:;">3rd menu item</a>
</a-menu-item>
</a-menu>
</a-dropdown>

<a-divider>侧边栏</a-divider>
<a-button onClick={this.controllerDrawer.bind(this)}>侧边栏控制</a-button>
<a-drawer

title="我是侧边标题"
placement="right"
closable={true}
onClose={this.controllerDrawer.bind(this)}
visible={this.showDrawer}>
<div>这里是侧边栏内部内容!</div>
</a-drawer>

<a-divider>全局提示</a-divider>

{

[0, 1, 2, 3].map((row, idx) => {
return (
<a-button
onClick={this.openGlobalTips.bind(this, row)}>{this.globalTips[row]}</a-button>
)
})
}
<a-divider>模态框</a-divider>
<a-button onClick={this.basic_modal_open.bind(this)}>打开基本模态框</a-button>

<a-modal
confirmLoading={this.modal_loading_flag}
title="Basic Modal"
v-model={this.basic_modal_flag}
onOk={this.basic_modal_ok.bind(this)}
>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</a-modal>

<a-divider>全局通知</a-divider>
<a-button onClick={this.submit_nitify.bind(this)}>通知1</a-button>
<a-divider>气泡确认</a-divider>
<a-popconfirm title="你确定吗?" confirm="confirm" cancel="cancel" okText="Yes" cancelText="No">
<a href="#">点击删除</a>
</a-popconfirm>
<a-divider>加载</a-divider>

<a-spin></a-spin>
<a-divider>···</a-divider>
<a-spin>
<div>
这里是内容区!
</div>
</a-spin>

<a-back-top/>


<div class = "footer">
<a-divider>这里是底线</a-divider>
</div>


</div>
)
},
data() {
return {
modal_loading_flag: false,
basic_modal_flag: false,
globalTips: [
'普通提示',
'加载提示',
'警告提示',
'错误提示'
],
showDrawer: false,
menu: {
template: "<a-menu><a-menu-item>001</a-menu-item></a-menu>"
}
}
},
methods: {
//发送通知
submit_nitify(type) {
console.log(type)
this.$notification.open({
message: '这是一条通知!',
description: '这是描述内容!.',
icon: <a-icon type="smile" style="color: #108ee9"/>,
onClick: () => {
console.log('你点击了通知');
},
});
},
//打开modal
basic_modal_open() {
console.log('打开Modal');
this.basic_modal_flag = !this.basic_modal_flag
},
//提交modal
basic_modal_ok() {
this.modal_loading_flag = true;
setTimeout(() => {
this.basic_modal_flag = !this.basic_modal_flag
this.$message.info("提交完成!")
}, 1500)
},
//全局提示
openGlobalTips(e) {
console.log(e)
switch (e) {
case 0 :
this.$message.info("这是一条普通的全局提示!")
break;
case 1 :
let task = this.$message.loading("这是一条加载提示!", 0)
setTimeout(task, 1000)

break;
case 2 :
this.$message.warn("这是一条警告提示!")
break;
case 3 :
this.$message.error("这是一条错误提示!")
break;
case 4 :
break;
default:
break;


}
},
controllerDrawer() {
this.showDrawer = !this.showDrawer
},
dropDownClick(e) {
console.log('点击下拉菜单' + e)
}
},
components: {}
}
</script>

<style scoped>
.scroll-box {
height: 1500px;
}
.footer{
position: fixed;
bottom: 0;
width: 100%;
left: 0;


}
.footer div{

padd-top: 50px;;

}

</style>

null

null

看一下效果:

null

null

null