Vue中的局部組件介紹
更新時間:2022年01月24日 14:25:22 作者:huxiaoxiao.
這篇文章主要介紹了Vue中的局部組件,文章圍繞Vue局部組件得相關資料展開內容,需要的的小孩伙伴請參考下面文章的具體介紹,希望對你有所幫助
在Vue中我們可以自己定義(注冊)局部組件
定義組件名的方式:
var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
然后在 components 選項中定義你想要使用的組件:
new Vue({
el: '#app',
// 組件中心
components: {
// 在視圖層渲染 局部注冊組件時
// component-a:你要在視圖層調用時使用的名稱
// ComponentA: 局部注冊組件名稱
'component-a': ComponentA,
'component-b': ComponentB
}
})
在視圖層調用局部組件:
<div id="app">
<component-a></component-a>
<component-b></component-b>
</div>
舉個列子:
<body>
<div id="app">
<component-a></component-a>
<component-b></component-b>
</div>
<script src="./js/vue.js"></script>
<script>
let componentA = {
template:`
<p>我是局部組件1</p>
`
}
let componentB = {
template:`
<p>我是局部組件2</p>
`
}
let vm = new Vue({
el:'#app',
data:{
},
components:{
"component-a":componentA,
"component-b":componentB
}
})
</script>
輸出結果為:

相關文章
vue中報錯“error‘xxx‘?is?defined?but?never?used”問題及解決
介紹了兩種解決代碼導入問題的方法:單一代碼解決和全局解決,第一種方法是在代碼前面添加特定代碼并保存;第二種方法是在package.json中添加代碼后重啟項目,這些方法可以有效解決導包錯誤提示,希望對大家有幫助2024-10-10
elementUI el-form 數(shù)據(jù)無法賦值且不報錯解決方法
本文主要介紹了elementUI el-form 數(shù)據(jù)無法賦值且不報錯解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12
Vuex報錯之[vuex] unknown mutation type: han
這篇文章主要介紹了Vuex報錯之[vuex] unknown mutation type: handlePower問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
vue3+ts+vite打包后靜態(tài)資源404無法加載js和css問題解決辦法
這篇文章主要給大家介紹了關于vue3+ts+vite打包后靜態(tài)資源404無法加載js和css問題的解決辦法,文中通過代碼以及圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-04-04

