console个性化应用

console简介

console 是一个用于调试和记录信息的内置对象, 提供了多种方法,可以帮助开发者输出各种信息,进行调试和分析。

console.log() 、console.info() :
输出信息,两者类似,但在某些浏览器中可能有不同的样式
image.png

console.warn() :
输出警告信息,通常会以黄色背景或带有警告图标的样式显示
image.png

console.error() :
输出错误信息,通常会以红色背景或带有错误图标的样式显示
image.png

痛点
上述可见打印出来的信息比较复杂,遇到大量复杂的数据打印观感弱辨识度低

技术方案

console.log() 可以接受任何类型的参数,包括字符串数字布尔值对象数组函数等。最厉害的是,它支持占位符!

常用的占位符:

  • %s - 字符串
  • %d or %i - 整数
  • %f - 浮点数
  • %o - 对象
  • %c - CSS 样式
格式化字符串

console.log() 支持类似于 C 语言 printf 函数的格式化字符串。我们可以使用占位符来插入变量值。

const name = 'Alice'; 
const age = 30; 
console.log('Name: %s, Age: %d', name, age); // Name: Alice, Age: 30
添加样式

可以使用 %c 占位符添加 CSS 样式,使输出内容更加美观。

console.log('%c This is a styled message', 'color: red; font-size: 20px;');

image.png

自定义样式的实现,其实主要是靠%c 占位符添加 CSS 样式实现的!

代码实现
// 实现方法
const createUseLog = () => {
  // 为空判断
  const isEmpty = (value) => {
    return value == null || value === undefined || value === "";
  };

  // 兼容提示
  const compatible = (title="", text="", color="#8b8b8b") => {
    let typef = Object.prototype.toString.call(text);
    if(typef === "[object String]") {
      console.log(
        `%c ${title} %c ${text} %c`,
        `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
        `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,
        "background:transparent"
      );
    } else {
      console.log(
        `%c ${title} %o`,
        `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
        text
      );
    }
  };

  // 提醒提示
  const info = (label="", content="") => {
    const title = isEmpty(content) ? "Info" : label;
    const text = isEmpty(content) ? label : content;
    compatible(title, text, "#8b8b8b");
  };

  // 错误提示
  const error = (label="", content = "") => {
    const title = isEmpty(content) ? "Error" : label;
    const text = isEmpty(content) ? label : content;
    compatible(title, text, "#c70000");
  };

  // 警告提示
  const warning = (label="", content = "") => {
    const title = isEmpty(content) ? "Warning" : label;
    const text = isEmpty(content) ? label : content;
    compatible(title, text, "#ffcd3a");
  };

  // 成功提示
  const success = (label="", content = "") => {
    const title = isEmpty(content) ? "Success " : label;
    const text = isEmpty(content) ? label : content;
    compatible(title, text, "#107010");
  };

  // 返回
  return {
    compatible,
    info,
    error,
    warning,
    success,
  };
};
// 创建打印对象
const log = createUseLog();

image.png

使用方法

  1. 下载依赖
npm i fgconsole
  1. 引用依赖
import fgconsole from 'fgconsole'
Vue.prototype.$fgconsole = fgconsole;
  1. 使用
this.$fgconsole.info("提示", "created");
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值