ESLint 校驗報錯 Do not use 'new' for side effects(no-new)


在 vue 項目中,ESLint 校驗報錯 Do not use 'new' for side effects. eslint(no-new). 錯誤圖片如下:

雖然項目還是可以運行,但不明白為啥報錯了,於是就翻了翻 eslint 文檔規則,ESLint 傳送門

禁止使用 new 以避免產生副作用 ( no-new )

對構造函數使用 new 的目的通常是創建一個特定類型的對象並且將該對象存儲在變量中,比如:var person = new Person();

使用 new 卻不存儲結果這種情況是不太常見的,比如:new Person() 。在這個例子中,創建的對象被銷毀因為它的引用沒有被存儲在任何地方,並且在許多場景中,這意味着構造函數應該被一個不需要使用 new 的函數所替代。

好吧,我犯的錯誤簡直不要太一樣 (/ω\) (/ω\) ~ . ~

SO ... ... eslint 設置此規則就是為了通過禁止使用 new 關鍵字調用構造函數但卻不將結果賦值給一個變量來保持一致性和約定。

好了,錯誤不可怕,只要有一個直面錯誤的心,正解也就不遠了,吼吼

嗯嗯,改正之后無錯誤提示,順利通過驗證。

 

除了上面這種方法,還可以屏蔽檢測,就是使用快速修復 Disable no-new for this line,如下所示:

這句注釋可以繞過檢測規則。雖然這個方法可行,不過還是建議找到問題所在。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 報錯 This is a development server. Do not use it in a production deployment.Use a production eslint 取消校驗 六、取消eslint 校驗代碼 如何關閉eslint語法校驗 eslint 報錯 vueJS 解決VSCode 中 報錯"You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file." 運行vue項目時報錯:You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. - Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as