Vue 第三方库的使用

本文介绍了在项目中使用第三方库的基本步骤,以ElementUI和Lodash为例。首先通过npm安装库,然后在Vue项目中导入并使用。对于ElementUI,讲解了安装CSS和JS文件,以及在main.js中启用组件。在Home.vue中展示了如何使用Icon和Button组件。对于Lodash,介绍了安装后通过import导入并在组件中使用其方法。
摘要由CSDN通过智能技术生成

1. 前言

本小节我们将带大家学习如何在项目中使用第三方库。在日常的开发中,我们正在大量的使用第三方库。学会使用第三方库可以说是前端工程师最基本的技能。其实,使用第三方库非常简单,绝大部分库的文档中都会教我们如何使用。接下来我们用几个案例来学习使用第三方库。

2. ElementUI 的使用

我们打开ElementUI的官网,根据官网的教程一步步学习。

2.1 安装

在 Vue-cli 创建的项目中,我们可以使用以下命令进行安装:

npm install element-ui -S

也可以通过 CDN 的方式在页面上直接引入:

<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

2.2 使用

在 main.js 中写入以下内容:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

Vue.config.productionTip = false;

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

此时,我们已经可以在项目中使用 Element 给我们提供的各种组件了。
我们可以改造 ‘views/Home.vue’ 中的内容:

<template>
  <div class="home">
    <h1>使用 icon 组件</h1>
    <i class="el-icon-edit"></i>
    <i class="el-icon-share"></i>
    <i class="el-icon-delete"></i>
    <h1>使用 button 组件</h1>
    <el-button>默认按钮</el-button>
    <el-button type="primary">主要按钮</el-button>
    <el-button type="success">成功按钮</el-button>
    <el-button type="info">信息按钮</el-button>
    <el-button type="warning">警告按钮</el-button>
    <el-button type="danger">危险按钮</el-button>
  </div>
</template>

<script>export default {
  name: "Home",
  components: {}
};</script>

3. Lodash 的使用

同样的,要使用 Lodash,我们需要先通过 npm install --save lodash 安装 Lodash。在使用 lodash 之前,通过 import _ from "lodash" 引入 Lodash 包。此时,我们就可以在项目中使用 Lodash 给我们提供方法了:

<script>
import _ from "lodash";
export default {
  name: "Home",
  created() {
    const str = _.join(["a", "b", "c"], "~");
    console.log(str);
  },
  components: {}
};
</script>

4. 小结

本节我们带大家学习了如何在项目中使用第三方库。在使用第三方库之前,我们需要先通过 npm install 安装第三方库,然后在项目中利用 import 加载第三方库。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值