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

    你可能感兴趣的文章
    Nginx 反向代理配置去除前缀
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>
    Nginx的是什么?干什么用的?
    查看>>
    Nginx访问控制_登陆权限的控制(http_auth_basic_module)
    查看>>
    nginx负载均衡的五种算法
    查看>>
    Nginx配置ssl实现https
    查看>>
    Nginx配置TCP代理指南
    查看>>
    Nginx配置代理解决本地html进行ajax请求接口跨域问题
    查看>>
    Nginx配置参数中文说明
    查看>>