Vue 第三方库的使用

怼烎@ 2024-03-30 18:02 229阅读 0赞

1. 前言

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

2. ElementUI 的使用

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

2.1 安装

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

  1. npm install element-ui -S

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

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

2.2 使用

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

  1. import Vue from "vue";
  2. import App from "./App.vue";
  3. import router from "./router";
  4. import store from "./store";
  5. Vue.config.productionTip = false;
  6. import ElementUI from "element-ui";
  7. import "element-ui/lib/theme-chalk/index.css";
  8. Vue.use(ElementUI);
  9. new Vue({
  10. router,
  11. store,
  12. render: h => h(App)
  13. }).$mount("#app");

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

  1. <template>
  2. <div class="home">
  3. <h1>使用 icon 组件</h1>
  4. <i class="el-icon-edit"></i>
  5. <i class="el-icon-share"></i>
  6. <i class="el-icon-delete"></i>
  7. <h1>使用 button 组件</h1>
  8. <el-button>默认按钮</el-button>
  9. <el-button type="primary">主要按钮</el-button>
  10. <el-button type="success">成功按钮</el-button>
  11. <el-button type="info">信息按钮</el-button>
  12. <el-button type="warning">警告按钮</el-button>
  13. <el-button type="danger">危险按钮</el-button>
  14. </div>
  15. </template>
  16. <script>export default {
  17. name: "Home",
  18. components: {}
  19. };</script>

3. Lodash 的使用

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

  1. <script>
  2. import _ from "lodash";
  3. export default {
  4. name: "Home",
  5. created() {
  6. const str = _.join(["a", "b", "c"], "~");
  7. console.log(str);
  8. },
  9. components: {}
  10. };
  11. </script>

4. 小结

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

发表评论

表情:
评论列表 (有 0 条评论,229人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue 使用

    1. 前言 本小节我们将带大家学习如何在项目中使用第三方库。在日常的开发中,我们正在大量的使用第三方库。学会使用第三方库可以说是前端工程师最基本的技能。其实,使用第三方库