博客
关于我
Vue 使用Use、prototype自定义全局插件
阅读量:454 次
发布时间:2019-03-06

本文共 1221 字,大约阅读时间需要 4 分钟。

开发环境配置与Vue全局插件自定义实现

Vue全局插件的创建与使用

一、开发环境配置

使用Win 10系统安装node.js环境

下载地址:node-v10.15.3-x64.msi

二、插件实现方式

  • 创建src目录下的plugin子目录

  • 在plugin目录下创建sendRequest.js文件

  • export function sendRequest() {console.log("send request by sendRequest Plugin");}

    1. 在plugin目录下创建customPlugin.js文件
    2. import sendRequest from "./sendRequest";export default sendRequest;

      1. 在plugin目录下创建index.js文件
      2. import customPlugin from "./customPlugin";const install = Vue => {if (install.installed) return;install.installed = true;Object.defineProperty(Vue.prototype, "$customPlugin", {get() {return customPlugin;}});};export default install;

        注意:Object.defineProperty方法用于在Vue原型上挂载插件对象

        五、在主文件中使用插件

        在main.js文件中添加以下内容:

        import Vue from "vue";import App from "./App";import router from "./router";import customPlugin from "@/plugin/index";

        Vue.use(customPlugin);Vue.config.productionTip = false;

        new Vue({el: "#app",router,components: { App },template: "

        "});

        六、在.vue组件中引用插件方法

        在methods中定义方法:

        methods: {sendRequest() {this.$customPlugin.sendRequest();}}

        关于插件调用方法

        可在js文件中直接引入customPlugin,使用Vue.prototype.$customPlugin.sendRequest()的方式调用

        需要注意的是,直接使用pluginName.fileModuleName.functionName()可能会导致fileModuleName为undefined的问题,建议使用Vue.prototype.$pluginName.fileModuleName.functionName()的方式调用

    转载地址:http://smvfz.baihongyu.com/

    你可能感兴趣的文章
    Oracle Java所有版本的下载链接
    查看>>
    Oracle JDBC url的几种方式
    查看>>
    oracle ogg 单实例双向复制搭建(oracle-oracle)--Oracle GoldenGate
    查看>>
    oracle ORA-14402 OGG-01296
    查看>>
    oracle partition by list,深入解析partition-list 分区
    查看>>
    Oracle PL/SQL Dev工具(破解版)被植入勒索病毒的安全预警及自查通告
    查看>>
    oracle rac集群的东西之QQ聊天
    查看>>
    Oracle Schema Objects——Tables——Table Compression
    查看>>
    oracle scott趣事
    查看>>
    oracle script
    查看>>
    Oracle select表要带双引号的原因
    查看>>
    Oracle SOA Suit Adapter
    查看>>
    Oracle Spatial GeoRaster 金字塔栅格存储
    查看>>
    Oracle spatial 周边查询SQL
    查看>>
    Oracle Spatial空间数据库建立
    查看>>
    UML— 活动图
    查看>>
    oracle sqlplus已停止工作,安装完成客户端后sqlplus报“段错误”
    查看>>
    oracle SQLserver 函数
    查看>>
    oracle sql分组(group,根据多个内容分组)在select之后from之前 再进行select查询,复杂子查询的使用
    查看>>
    Oracle Statspack分析报告详解(一)
    查看>>