博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为 Vue 编写一个插件
阅读量:4086 次
发布时间:2019-05-25

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

1 介绍

以编写Vue日志插件为例,讲述从插件的开发到部署。

原文  

2 代码初始

不用一步到位开发插件,先抛开 Vue保证自己的代码能够运行

var logger = {  debug: false,  prefix: 'Yunhuni:'}let levels = ['log', 'info', 'warn']for (let level of levels) { logger[level] = function() {  if (!this.debug) return;  if (typeof console == "undefined") return;  var slice = Array.prototype.slice;  var args = slice.call(arguments, 0);  args.unshift(this.prefix + level);  console[level].apply(console, args); }}logger.log('aaaa')logger.info('aaaa')logger.warn('aaaa')

3 编写成Vue的插件

上面代码能跑了,然后再根据  接入我们的代码

const vLogger = {}vLogger.install = function (Vue, options) {  if (vLogger.installed) return  var logger = {    dev: true,    prefix: '',    levels: ['log', 'warn', 'debug']  }  if (options) {    for (const key of Object.keys(options)) {      if (key === 'levels') {        logger[key] = logger[key].concat(options[key])      } else {        logger[key] = options[key]      }    }  }  for (const level of logger.levels) {    logger[level] = function () {      if (!this.dev || typeof console === 'undefined') return      var args = Array.from(arguments)      args.unshift(`[${
this.prefix} :: ${level}]`.toUpperCase()) console[level].apply(console, args) } } Vue.prototype.$log = logger Vue.log = logger}export default vLogger

4 使用

import vueLogger from './logger'Vue.use(vueLogger, { prefix: new Date(), dev: true })// @test.vuethis.$log.log('hello vue log') // => [MON DEC 05 2016 15:35:00 GMT+0800 (CST) :: LOG] hello world

4.1 参数

name type default
prefix string none
dev boolean true
levels array ['log', 'warn', 'default']

5 编写测试用例

使用jasmine,这里以测试参数 options 为例子

// 测试 参数 levelimport Vue from 'vue'import Logger from '../../src/index.js'describe("this.$log", function() {  Vue.use(Logger)  const vm = new Vue()  const str = 'hello world'  it("level log out hello world", function() {    expect(vm.$log.log).toBeDefined()    vm.$log.log = jasmine.createSpy('log')    vm.$log.log(str)    expect(vm.$log.log).toHaveBeenCalledWith(str);  });  it("level debug out hello world", function() {    expect(vm.$log.debug).toBeDefined()    vm.$log.debug = jasmine.createSpy('debug')    vm.$log.debug(str)    expect(vm.$log.debug).toHaveBeenCalledWith(str);  });  describe("Vue log", function() {    it("level debug out hello world", function() {      expect(vm.$log.debug).toBeDefined()      Vue.log.debug = jasmine.createSpy('debug')      Vue.log.debug(str)      expect(Vue.log.debug).toHaveBeenCalledWith(str);    });  });});

6 部署

6.1 如何选择开源许可证

参考阮老师的 

6.2 添加项目徽章

通过这些微章简单直白的了解该项目的状态。 可以在这个  获取你想要svg,一般格式如下

![Ci](https://img.shields.io/circleci/project/github/Lluvio/vue-logger.svg)

想要图标点击可跳转

[![Ci](https://img.shields.io/circleci/project/github/Lluvio/vue-logger.svg)](https://circleci.com/gh/Lluvio/vue-logger)

7 发布

首先需要在本地添加 npm 用户

# 账号密码和你在 npm 官网注册的账号一致npm adduser# 然后登入npm login

如果想要指定特定标签,参考 

7.1 publish 失败

出现以下错误,有可能是代理地址错误,每个命令后都需要带上 --registry http://registry.npmjs.org

no_perms Private mode enable, only admin can publish this module

8 最后

最终结果在  , 欢迎指正!

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

你可能感兴趣的文章
Socket经验记录
查看>>
对RTMP视频流进行BitmapData.draw()出错的解决办法
查看>>
FMS 客户端带宽计算、带宽限制
查看>>
在线视频聊天(客服)系统开发那点事儿
查看>>
SecurityError Error 2148 SWF 不能访问本地资源
查看>>
Flex4的可视化显示对象
查看>>
Flex:自定义滚动条样式/隐藏上下箭头
查看>>
烈焰SWF解密
查看>>
Qt 静态编译后的exe太大, 可以这样压缩.
查看>>
3D游戏常用技巧Normal Mapping (法线贴图)原理解析——基础篇
查看>>
乘法逆元
查看>>
STL源码分析----神奇的 list 的 sort 算法实现
查看>>
Linux下用math.h头文件
查看>>
Linux中用st_mode判断文件类型
查看>>
Ubuntu修改host遇到unable to resolve host
查看>>
路由选择算法
查看>>
Objective-C 基础入门(一)
查看>>
Objective-C 基础入门(三) 读写文件与回调
查看>>
C++ STL标准库与泛型编程(一)概述
查看>>
C++ STL标准库与泛型编程(四)Deque、Queue、Stack 深度探索
查看>>