在vue項目中使用canvas-nest.js,報parameter 1 is not of type 'Element'

canvas-nest.js是一款輕量的網頁特效,如圖:html

 

github地址:https://github.com/hustcc/canvas-nest.jsvue

在普通的html項目中,只要將<script src="dist/canvas-nest.js"></script>插入到body標籤最下邊便可。git

在vue項目中,使用時配置github

 1 import CanvasNest from 'canvas-nest.js';
 2 
 3 const config = { // 配置
 4     color: '255, 0, 0', // 線條顏色
 5     pointColor: '255, 155, 0', // 節點顏色
 6     opacity: 1, // 線條透明度
 7     count: 222, // 線條數量
 8     zIndex: 99 // 畫面層級
 9 };
10 
11 // Using config rendering effect at 'element'.
12 // element爲html的dom對象,如id爲body的dom爲:document.getElementById('body');
13 const cn = new CanvasNest(element, config);
14 
15 // destroy
16 cn.destroy();

可是在vue項目中,引入canvas-nest.js後,直接在created中 new CanvasNest(element, config);的話,可能會報下圖的錯誤,不顯示效果canvas

緣由是dom沒有渲染完畢,就開始使用element。dom

解決辦法:在created中this

1 this.$nextTick(()=> {
2     const cn = new CanvasNest(element, config);
3 })

這樣寫一個延時操做就能夠了,固然也能夠使用setTimeout。spa

總結,遇到parameter 1 is not of type 'Element' 這樣類型的報錯,須要檢查dom是否渲染完畢。3d

相關文章
相關標籤/搜索