vue 引入第三方组件库 vant
1. vant 简介
vant 是有赞开源的移动端组件库。
官网 https://youzan.github.io/vant/#/zh-CN/quickstart (需要fq)
2. 安装引入
# 通过 npm 安装
npm i vant -S
或者
# 通过 yarn 安装
yarn add vant
引入
babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式
# 安装插件
npm i babel-plugin-import -D
在babel.config.js 文件添加
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
\\ 下面这段是引入的
\\开始
plugins: [
['import', {
libraryName: 'vant',
libraryDirectory: 'es',
style: true
}, 'vant']
]
\\结束
}
3. 使用
创建一个contactList.vue
<template>
<van-button type="primary">主要按钮</van-button>
</template>
<script>
import {Button} from 'vant' //引入组件
export default {
name: "contactList",
components: {
[Button.name]:Button // 使用组件
},
}
</script>
<style scoped>
</style>
<template>
<div id="app">
<index></index>
<contactList></contactList>
</div>
</template>
<script>
import index from "@/components/index";
import contactList from "@/components/contactList"; //导入contactList.vue
export default {
name: 'App',
components: {
index,
contactList // 使用contactList
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
4. 启动项目
npm run serve