Angular中使用Vis.js设置节点背景色报错:background不存在
解决Vis.js 4.x版本节点背景色设置问题
在你使用的Vis.js 4.21.0-EOL版本中,节点背景色的设置需要注意color属性的类型:
如果仅需要设置背景色,直接将
node.color赋值为颜色字符串即可,此时边框会自动生成对比色:node.color = '#0BB176';如果需要同时自定义背景色和边框色,需要确保
node.color是一个对象(若原本是字符串类型,需先转为对象),再设置对应属性:// 先将color转为对象(避免字符串类型下访问background报错) if (typeof node.color !== 'object') { node.color = {}; } node.color.background = '#0BB176'; node.color.border = '#333333'; // 可选,自定义边框颜色
或者直接覆盖整个color对象:
node.color = { background: '#0BB176', border: '#222222' };
你之前报错“background不存在”,大概率是因为node.color初始值是字符串类型,而非对象,直接访问字符串的background属性自然会报错。
内容的提问来源于stack exchange,提问作者Donovant
相关产品推荐
相关产品推荐

