vue中按需使用第三方插件

摘要:
基于vue-cli31.安装元素ui并按需加载插件cnpminstallelementuibabel-plugin-component-save-dev2。添加配置,在根目录中创建babelrc,然后重新启动服务{“presets”:[[“es2015”,{“modules”:false}]],“plugins”:[[“component”,{“libraryName”:“element

基于vue-cli3

1.安装element-ui 和按需加载插件

cnpm install element-ui babel-plugin-component --save-dev

2.添加配置,在根目录新建babelrc,需要重启服务

{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "thecnpmme-chalk"
      }
    ]
  ]
}

3.在main.js引入element-ui的message组件

import { Message  } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

4.插件有两种使用方式,一种是在组件中需要使用插件的地方按需引入

 import { Message  } from 'element-ui';

 delProduct(item){
      this.axios.delete(`/carts/${item.productId}`).then((res)=>{
        Message.success('删除成功');
        this.renderData(res);
      });
   },   

5.另一种是使用prototye把插件的对象扩展进来  

main.js:
// 通过原型的方式扩展对象,绑定message: Vue.prototype.$message=Message;

如果是组件的话可以绑定全局component,具体参照官方文档
https://element.eleme.cn/#/zh-CN/component/quickstart
Vue.component(Button.name, Button);

6.使用方式

 this.axios.post('/user/register',{
    username:'admin1',
    password:'admin1',
    email:'admin1@163.com'
}).then(()=>{
   this.$message.success('注册成功')
})  

  

 

  

免责声明:文章转载自《vue中按需使用第三方插件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux内核的makefile.txt讲解[转]innerHtml,innerText,outterHtml,outterText 的区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue中的computed属性

参考:https://www.cnblogs.com/gunelark/p/8492468.html 看了网上很多资料,对vue的computed讲解自己看的都不是很清晰,今天忙里抽闲,和同事们又闲聊起来,对computed这个属性才有了一个稍微比较清晰的认识,下面的文章有一部分是转自: https://www.w3cplus.com/vue/vue-c...

Vue使用ref 属性来获取DOM

注意,在父组件中可以使用this.$refs.属性名  获取任何元素的属性和方法,子组件不可以获取父组件中的 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"...

vue中记录页面的滚动距离

业务需求:pageOne页面是一个商品列表页面,在这个页面点击商品,就会跳转到pageTwo商品详细页面。此时再从pageTwo页面返回到pageOne页面时,pageOne页面需要做到:1.记录pageOne之前的滚动的距离。2.不重新请求数据。而从其它页面进入到pageOne页面时,pageOne页面不需要记录之前的滚动距离和需要重新请求数据。 1.使...

eslint使用

vscode配置 需安装eslint和vetur两个插件 原因: 装eslint并配置:编辑时就可提示报错信息,保存可自动修复 vetur:会有一些vue语法提示 配置eslint 1.文件=》首选项=》设置 2.在settings.json中添加 也可在项目跟目录添加.vscode文件,里头添加settings.json,内容为: { // 每次保存...

基于Vue的页面切换左右滑动效果

HTML文本页面: <template> <div id="app> <transition :name="direction" mode="out-in"> <!--动态获得transition 的name值--> <router-view class="app-view...

【Vue】 编写Vue插件流程

一、在Vue中编写插件流程 1、创建组件 components/message.vue <template> <div v-if="isShow"> <span>{{message}}</span> </div> </template> <...