html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總
作者:Echoyya
出處:https://www.cnblogs.com/echoyya/
最近在寫項目的時候,總是遇到在html中使用vue.js的情況,且頁面邏輯較多,之前的項目經(jīng)驗都是使用腳手架等已有的項目架構(gòu),使用.vue文件完成組價注冊,及組件之間的調(diào)用,還沒有過在html中創(chuàng)建組件的經(jīng)驗,所以借此機會學(xué)習(xí)總結(jié)一下。
方法一:Vue.extend( options )
- 用法:使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個“子類”。參數(shù)是一個包含組件選項的對象。data 選項是特例,需要注意 - 在 Vue.extend() 中它必須是函數(shù)
- extend 創(chuàng)建的是 Vue 構(gòu)造器,而不是我們平時常寫的組件實例,所以不可以通過 new Vue({ components: testExtend }) 來直接使用,需要通過 new Profile().$mount('selector選擇器') 來掛載到指定的元素上。
- Vue.extend + vm.$mount 組合
// 借用官網(wǎng)的例子,小小改動了一下
// 在父組件中,創(chuàng)建一個子組件,并調(diào)用
<div id='app'>
<button>{{message}}</button>
<div id="mount-point"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el:"#app",
data:{
message:'父組件'
},
});
// 創(chuàng)建構(gòu)造器
var Profile = Vue.extend({
template: '<p>{{firstName}} {{lastName}} {{alias}}</p>',
data: function () {
return {
firstName: 'N',
lastName: 'H',
alias: 'Y'
}
}
})
// 創(chuàng)建 Profile 實例,并掛載到一個元素上。
new Profile().$mount('#mount-point')
</script>
方法二:Vue.component( id, [definition] ) + Vue.extend( options )
用法:Vue.component()注冊或獲取全局組件。注冊還會自動使用給定的 id 設(shè)置組件的名稱
<div id="app">
<!-- 如果要使用組件,直接把組件的名稱以 HTML 標(biāo)簽的形式,引入到頁面中-->
<todo :todo-data="todoList"></todo>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
// 構(gòu)建一個子組件
var todoItem = Vue.extend({
template: ` <li> {{ text }} </li> `,
props: ['text']
})
// 構(gòu)建一個父組件
var todoWarp = Vue.extend({
template: `
<ul>
<todo-item v-for="(item, index) in todoData" v-text="item.text"></todo-item>
</ul>
`,
props: ['todoData'],
// 局部注冊子組件
components: {
//使用 components 定義組件時,若組件名稱使用駝峰命名,則在引用組件時,需要把大寫改為小寫,并且用'-'將單詞連接
todoItem: todoItem
}
})
// 注冊到全局
Vue.component('todo', todoWarp) // 等同于下面這種寫法
Vue.component('todo',Vue.extend({
template : 'xxx',
props:[xxx],
components:'xxx'
}))
new Vue({
el: '#app',
data: {
todoList: [
{ id: 0, text: '工作' },
{ id: 1, text: '學(xué)習(xí)' },
{ id: 2, text: '休息' }
]
}
})
</script>
方法三:直接使用Vue.component()
<div id="app">
<mycom></mycom>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
Vue.component('mycom',{
template : '<h3>這是使用 Vue.component 創(chuàng)建的組件</h3>'
})
new Vue({
el: '#app'
})
</script>
但是這樣寫會有一個問題:在h3標(biāo)簽后出現(xiàn)另一個標(biāo)簽,就會出問題,
Vue.component('mycom',{
template : '<h3>這是使用 Vue.component 創(chuàng)建的組件</h3><h3>這是使用 Vue.component 創(chuàng)建的組件</h3>'
})

- 原因:組件template屬性指向的模板內(nèi)容,必須有且只能有唯一的一個根元素
- 解決方法: 用唯一的div作為父元素,將多個子元素包裹
方法四:使用Vue.component()
在被控制的 #app 外面使用 template 元素,定義組建的HTML模板結(jié)構(gòu)
<div id="app">
<mycom></mycom>
</div>
<template id="tem1">
<h1>這是 template 元素</h1>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
Vue.component('mycom', {
template: '#tem1'
});
new Vue({
el: '#app'
})
</script>
方法五:使用Vue.component() + is
<div id="app">
<ul>
<li is="todo-item" v-for="(todo,index) in todos " :title="todo" :key="index" @remove="removeTodo(index)"></li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript">
Vue.component('todo-item', {
template: `
<li>
{{title}}
<button @click="$emit('remove')">remove</button>
</li>
`,
props: ['title']
})
new Vue({
el: "#app",
data: {
todos: ["eating", "swimming", "reading"]
},
methods: {
removeTodo: function (index) {
this.todos.splice(index, 1)
}
}
})
補充說明一下is屬性:
有些 HTML 元素,諸如 ul、ol、table 和 select,對于可以出現(xiàn)在其內(nèi)部元素是有嚴(yán)格限制的。而有些元素,諸如 li、tr 和 option,只能出現(xiàn)在特定的元素內(nèi)部。這會導(dǎo)致我們使用這些有約束條件的元素時遇到一些問題。例如
<table> <blog-post-row></blog-post-row> </table>
這個自定義組件 會被作為無效的內(nèi)容提升到外部,并導(dǎo)致最終渲染結(jié)果出錯。幸好這個特殊的 is attribute 給了我們一個變通的辦法:
<table> <tr is="blog-post-row"></tr> </table>
以上就是html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于html 創(chuàng)建調(diào)用vue組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3中reactive數(shù)據(jù)被重新賦值后無法雙向綁定的解決
這篇文章主要介紹了vue3中reactive數(shù)據(jù)被重新賦值后無法雙向綁定的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
vue-router實現(xiàn)tab標(biāo)簽頁(單頁面)詳解
這篇文章主要為大家詳細(xì)介紹了vue-router實現(xiàn)tab標(biāo)簽頁的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
element-ui中如何給el-table的某一行或某一列加樣式
本文主要介紹了element-ui中怎么給el-table的某一行或某一列加樣式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

