[英]Javascript: console.log with colors and objects
我知道我可以通過執行將 colors 添加到我的 JS/TS 日志記錄中
const msg = 'My Message';
console.debug(`%c${msg}`, 'color: green; font-weight: bold;');
但是我如何也用顏色記錄對象呢? 我嘗試了一些實驗
const obj = {key: 'value'};
console.debug(`%c${msg}`, 'color: green; font-weight: bold;',obj );
console.debug(`%c${msg}`, ...['color: green; font-weight: bold;',obj]);
但是它們都產生相同的奇怪的無色格式
只需使用JSON.stringify()
將其轉換為彩色文本
const obj = {key: 'value'};
console.log(`%c ${JSON.stringify(obj)}`, 'color: green; font-weight: bold;');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.