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

官方文檔已經(jīng)給我們描述了兩種使用情況,可能在開發(fā)中,并不是常用,舉例子說明一下。
1.組件自身的遞歸調(diào)用
就是在當前組件中,調(diào)用組件自己
componentA.vue
<template>
<div class="component-a">
<!-- 一個簡單的樹形組件 -->
<tree :treeData="treeData"></tree>
</div>
</template>
<script>
export default {
name: 'component-a',
data() {
return {
treeData: [{
title: '樹形標題一',
expand: true,
children: [{
title: '子標題1',
expand: true
},
{
title: '子標題2',
expand: true,
children: [{
title: '子標題2.1',
expand: true
},
{
title: '子標題2.2',
expand: true
},
{
title: '子標題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.當我們使用vue.js官方提供的調(diào)試工具調(diào)試
可以看到組件的名稱,更好地定位

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

這就是vue.js中組件export default 中name的三種作用。調(diào)試和keep-alive是我們開發(fā)中常用的功能,關于組件的遞歸調(diào)用,還是第一次實踐,遞歸時,大家一定要注意遞歸的條件,否則會進入死循環(huán)。
Vue如何獲取組件name屬性
Vue在編寫組件時一般都會顯式的指明其name屬性
獲取name屬性
this.$options.name
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

