Vue 使用Useprototype自定义全局插件

 

by:授客 QQ1033553122

 

开发环境

 

Win 10

 

node-v10.15.3-x64.msi

下载地址:

https://nodejs.org/en/

实现方式1

1.   src目录下新建plugin目录

2.   plugin目录下新建sendReuest.js

export function sendRequest() {

console.log("send request by sendRequet Plugin")

}

3.   plugin目录下新建customPlugin.js

import * as customPlugin from"./sendRequest"

export default customPlugin

4.   plugin目录下新建index.js

// 导入所有接口

import customPlugin from"./customPlugin"

const install = Vue=> {

if (install.installed) return// 如果已经注册过了,就跳过

install.installed = true

Object.defineProperties(Vue.prototype, {

// 注意,此处挂载在 Vue 原型的 $customPlugin对象上

$customPlugin: {

get() {

return customPlugin

}

}

})

}

export default install

 

关于Object.defineProperty

这个函数接受三个参数,一个参数是obj,表示要定义属性的对象,一个参数是prop,是要定义或者更改的属性名字,另外是descriptor,描述符,来定义属性的具体描述。

Object.defineProperty(obj, prop, descriptor)

5.   修改main.js

如下,新增带背景色部分的内容

// The Vue build version to load with the `import` command

// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from "vue"

import App from "./App"

import router from "./router"

import customPlugin from "@/plugin/index"

//等价导入方式

// import customPlugin from "@/plugin/"

// import customPlugin from "@/plugin "

Vue.use(customPlugin)

Vue.config.productionTip = false

/* eslint-disable no-new */

new Vue({

el:"#app",

router,

components: { App },

template:"<App/>"

})

 

6.   .vue组件中引用

.vue组件使用对应的插件调用sendReuqest方法

methods: {

sendRequest() {

this.$customPlugin.sendRequest();

}

},

 

注意:也可以在某些js中直接引入customPlugin,customPlugin.sendRequest()的方式使用,笔者某次实践时这么使用:pluginName.fileModuleName.functionName(),发现会报错,提示fileModuleNameundefined,解决方法:采用Vue.prototype.$pluginName.fileModuleName.functionName()进行调用。

实现方式2

类似实现方式1,不同的地方在于:

1、去掉第4步

2、第5步,在main.js中添加的内容变成如下

import customPlugin from "@/plugin/customPlugin"

...略

Vue.prototype.$customPlugin = customPlugin

 

参考链接

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

 

Vue 使用Use、prototype自定义全局插件的更多相关文章

  1. 【vue3】封装自定义全局插件

    [vue3]封装自定义全局插件 原vue2方法 main.js import Vue from 'vue' import App from './App.vue' import router from ...

  2. 07vue 自定义全局组件 通用流程

    1.全局组件的目录 2.loading/index.js import LoadingComp from './Loaiding' const compName=LoadingComp.name // ...

  3. Vue 使用use、prototype自定义自己的全局组件

    使用Vue.use()写一个自己的全局组件. 目录如下: 然后在Loading.vue里面定义自己的组件模板 <template> <div v-if="loadFlag& ...

  4. 开发vue全局插件的4种方式

    定义全局插件的步骤 定义全局插件 pluginsUtil.js Vue.js 的插件应当有一个公开方法 install .这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象: ex ...

  5. vue 自定义全局方法

    import {myfun} from '../static/js/test.js' //se6的正确写法export default {methods:{ diyfun:function () { ...

  6. vue自定义全局和局部指令

    一.介绍 1.除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令. 2.自定义指令的分类       1.全局指令 2.局部指令 3.自定义全局指令格式 V ...

  7. vue.js过度&动画、混入&插件

    1.vue  过度动画 1.过度 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.Vue 提供了内置的过渡封装组件,该组件用于包裹要实现过渡效果的组件. 语法格式: < ...

  8. Vuejs自定义全局组件--loading

    不管是使用框架,还是不使用任何的框架,我们都不可避免的需要与“加载中……”打交道,刚刚学习了Vuejs自定义组件的写法,就现学现卖,介绍一下吧! 先看一下目录结构,一般情况下,每一个组件都新建一个新的 ...

  9. 第二章 Vue快速入门-- 28 自定义按键修饰符

    事件处理-按键修饰符 js 里面的键盘事件对应的键码 <!DOCTYPE html> <html lang="en"> <head> <m ...

  10. vue学习-day02(自定义指令,生命周期)

    目录: 1.案例:品牌管理    2.Vue-devtools的两种安装方式    3.过滤器,自定义全局或私有过滤器    4.鼠标按键事件的修饰符    5.自定义全局指令:让文本框获取焦点   ...

随机推荐

  1. 启动Django项目的方式

    方式一: python manage.py runserver 方式二: # 加上监听地址和端口 python manage.py runserver 0.0.0.0:8080 方式三: 使用 Pyc ...

  2. Android 13 - Media框架(20)- ACodec(二)

    关注公众号免费阅读全文,进入音视频开发技术分享群! 这一节开始我们就来学习 ACodec 的实现 1.创建 ACodec ACodec 是在 MediaCodec 中创建的,这里先贴出创建部分的代码: ...

  3. 第一次线上 OOM 事故,竟和 where 1 = 1 有关

    这篇文章,聊聊一个大家经常使用的编程模式 :Mybatis +「where 1 = 1 」. 笔者人生第一次重大的线上事故 ,就是和使用了类似的编程模式 相关,所以印象极其深刻. 这几天在调试一段业务 ...

  4. LeetCode 146. LRU CacheLRU缓存机制 (C++/Java)

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  5. The solution of P3012

    problem & blog 很明显是个 DP. 于是我们定义 \(dp_{i,j,k}\) 为末尾的字符的 ASCII 码为 \(i\),有 \(j\) 个大写字母,\(k\) 个小写字母. ...

  6. 第三届机器人、人工智能与信息工程国际学术会议(RAIIE 2024)

    [ACM独立出版/Fellow大咖云集]2024年第二届机器人.人工智能与信息工程国际学术会议(RAIIE 2024) 2024 3rd International Symposium on Robo ...

  7. flutter 尝试创建第一个页面(三)

    新建目录 assets  存放图片 在pubspec..yaml 中添加 flutter: # The following line ensures that the Material Icons f ...

  8. Go 语言中的异常处理简单实践 panic、recover【GO 基础】

    〇.Go 中的异常处理简介 Golang 没有结构化异常,使用 panic 抛出错误,recover 捕获错误. panic.recover 参数类型为 interface{},因此可抛出任何类型对象 ...

  9. 报错解决 :Resolved [org.springframework.web.bind.MissingServletRequestParameterException

    报错解决 :Resolved [org.springframework.web.bind.MissingServletRequestParameterException 解决方法:RequestPar ...

  10. redis简单应用demo - 订单号自增长的思路:业务编码+地区+自增数值

    redis简单应用demo1.字符串127.0.0.1:6379> set hello toneyOK127.0.0.1:6379> type hellostring127.0.0.1:6 ...