博客
关于我
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/

    你可能感兴趣的文章
    Perl的基本語法
    查看>>
    perl输出中文有乱码
    查看>>
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 大数据ssh权限问题 hadoop起不来 hadoopssh错
    查看>>
    PermissionError:Python 中的 [Errno 13]
    查看>>
    PermissionError:[Errno 13] 权限被拒绝:‘/manage.py‘
    查看>>
    Permutation
    查看>>
    perspective意思_2020年12月英语四级词汇讲解丨考点归纳:perspective
    查看>>
    PE文件,节头有感IMAGE_SECTION_HEADER
    查看>>
    PE查找文件偏移地址
    查看>>
    PE知识复习之PE的导入表
    查看>>
    PFX(Parallel Framework) and Traditional Multithreading
    查看>>
    PGOS:今天动手给电脑装青苹果Win7 X64位系统
    查看>>
    pgpool-II3.1 的内存泄漏(一)
    查看>>
    PgSQL · 特性分析 · PG主备流复制机制
    查看>>
    PGSQL主键序列
    查看>>
    PGSQL安装PostGIS扩展模块
    查看>>
    pg数据库中两个字段相除
    查看>>
    PhalApi:[1.23] 请求和响应:GET和POST两者皆可得及超越JSON格式返回
    查看>>
    Phalcon环境搭建与项目开发
    查看>>
    Phantom.js维护者退出,项目的未来成疑
    查看>>