vue組件使用駝峰命名和短橫線命名問題(導致沒有找到組件)


轉自:https://blog.csdn.net/weixin_44198965/article/details/99607987

前言

最近使用 vue 組件時,名稱使用了駝峰命名之后,組件寫在 html 代碼段中卻拋出錯誤。

HTML:

<div id="app"> <newButton></newButton> </div> 
  • 1
  • 2
  • 3

JavaScript:

Vue.component('newButton',{ data () { return {} }, template:'<button>示例按鈕</button>' }) new Vue({el:'#app'}) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

錯誤:
在這里插入圖片描述
vue給出的錯誤提示沒有找到組件,但事實是由駝峰命名所引起。
vue給出的錯誤提示是:沒有注冊組件?

解決方法

在書寫組件 HTML 代碼段中,將大寫字母改為小寫,然后在前面加一個 " - " 號連接即可。

HTML:

<div id="app"> <new-button></new-button> </div> 
  • 1
  • 2
  • 3

JavaScript:

Vue.component('newButton',{ data () { return {} }, template:'<button>示例按鈕</button>' }) new Vue({el:'#app'}) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在這里插入圖片描述

 


免責聲明!

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



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM