Vue?export?default中的name屬性有哪些作用
Vue export default的name屬性作用
在劃分模塊和創(chuàng)建單頁(yè)面組件時(shí),常常寫到name。嵌套路由中,index.vue極為常見。
那么在vue中,export default { name: ‘xxx’} 中的name到底有啥作用呢?
還是先回到官方的文檔:官方鏈接

官方文檔已經(jīng)給我們描述了兩種使用情況,可能在開發(fā)中,并不是常用,舉例子說明一下。
1.組件自身的遞歸調(diào)用
就是在當(dāng)前組件中,調(diào)用組件自己
componentA.vue
<template>
<div class="component-a">
<!-- 一個(gè)簡(jiǎn)單的樹形組件 -->
<tree :treeData="treeData"></tree>
</div>
</template>
<script>
export default {
name: 'component-a',
data() {
return {
treeData: [{
title: '樹形標(biāo)題一',
expand: true,
children: [{
title: '子標(biāo)題1',
expand: true
},
{
title: '子標(biāo)題2',
expand: true,
children: [{
title: '子標(biāo)題2.1',
expand: true
},
{
title: '子標(biāo)題2.2',
expand: true
},
{
title: '子標(biāo)題2.3',
expand: true
}]
}]
}
},
components: {
// 自定義組件
tree: {
// 組件的名稱
name: 'tree',
// 模板
template: `
<ul>
<li v-for="item in treeData">
<span>{{item.title}}</span>
<!-- 在組件內(nèi)部調(diào)用自己 -->
<tree v-if="item.children" :treeData="item.children"></tree >
</li>
</ul>`,
// 通過父組件傳遞的數(shù)據(jù)
props: ['treeData']
}
},
methods: {}
}
</script>

2.當(dāng)我們使用vue.js官方提供的調(diào)試工具調(diào)試
可以看到組件的名稱,更好地定位

3.最后一種應(yīng)該是使用比較多的情況
就是當(dāng)我們使用 keep-alive時(shí)可以使用include和exclude指定需要緩存和不需要緩存的組件。指定的依據(jù)就是組件的name。

這就是vue.js中組件export default 中name的三種作用。調(diào)試和keep-alive是我們開發(fā)中常用的功能,關(guān)于組件的遞歸調(diào)用,還是第一次實(shí)踐,遞歸時(shí),大家一定要注意遞歸的條件,否則會(huì)進(jìn)入死循環(huán)。
Vue如何獲取組件name屬性
Vue在編寫組件時(shí)一般都會(huì)顯式的指明其name屬性
獲取name屬性
this.$options.name
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue--點(diǎn)擊當(dāng)前增加class,其他刪除class的方法
今天小編就為大家分享一篇vue--點(diǎn)擊當(dāng)前增加class,其他刪除class的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue使用Cropper實(shí)現(xiàn)圖片裁剪功能
圖片裁剪功能無(wú)論是用戶頭像的裁剪,還是圖片內(nèi)容的精確調(diào)整,都成為了提升用戶體驗(yàn)的關(guān)鍵一環(huán),本文將詳細(xì)介紹如何在Vue.js項(xiàng)目中集成并使用Cropper.js實(shí)現(xiàn)一個(gè)強(qiáng)大的圖片裁剪組件,需要的可以參考下2024-11-11

