Vue兩種組件類型:遞歸組件和動態(tài)組件的用法
一遞歸組件
遞歸組件的特性就是可以在自己的template模板中調(diào)用自己本身。值得注意的它必須設(shè)置name屬性。
// 遞歸組件 recursive.vue
<template>
<div>
<p>遞歸組件</p>
<Recursion :count="count + 1" v-if="count < 3"></Recursion>
</div>
</template>
<script>
export default {
name: "Recursion",//必須設(shè)置name屬性
props: {
count: {
type: Number,
default: 1
}
}
}
</script>
這個例子中父頁面使用該遞歸組件會調(diào)用三次recursive組件,值得注意的是遞歸組件必須設(shè)置遞歸次數(shù)限制數(shù)量
否則會拋出錯誤,該例子中通過count來限制遞歸次數(shù)。
二 動態(tài)組件
如果將一個Vue組件命名為Component會報錯,因為Vue提供來特殊的元素<component>來動態(tài)掛載不同組件。
并使用is特性來選擇要掛載的組件。
// parentComponent.vue
<template>
<div>
<h1>父組件</h1>
<component :is="currentView"></component>
<button @click = "changeToViewB">切換到B視圖</button>
</div>
</template>
<script>
import ComponentA from '@/components/ComponentA'
import ComponentB from '@/components/ComponentB'
export default {
components: {
ComponentA,
ComponentB
},
data() {
return {
currentView: ComponentA // 默認(rèn)顯示組件 A
}
},
methods: {
changeToViewB () {
this.currentView = ComponentB // 切換到組件 B
}
}
}
</script>
通過改變currentView的值就可以動態(tài)切換顯示的組件,與之類似的是vue-router的實現(xiàn)原理,前端路由到不同的頁面實際上就是加載不同的組件。
補(bǔ)充知識:Vue route部分簡單高級用法
一、改變頁面title的值
在開發(fā)時常常需要在切換到不同頁面時改變?yōu)g覽器的title值,那么我們就可以在定義路由的時候通過配置 meta 屬性
來改變title值。
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import UserInfo from ".././userInfo.vue";
import ChangeCommunity from ".././ChangeCommunity.vue";
var vueRouter= new Router({
routes: [
{
path: '/',
name: 'UserInfo',
component: UserInfo,
meta: {
title: '我的信息'
}
},
{
path: '/ChangeCommunity',
name: 'ChangeCommunity',
component: ChangeCommunity,
meta: {
title: '我的社區(qū)'
}
},
]
})
vueRouter.beforeEach((to, from, next) => {
/* 路由發(fā)生變化修改頁面title */
if (to.meta.title) {
document.title = to.meta.title;
}
next();
})
export default vueRouter
當(dāng)從我的信息頁面跳轉(zhuǎn)到我的社區(qū)頁面時,對應(yīng)的title值也會由“我的信息”變成“我的社區(qū)”。
二、路由懶加載
當(dāng)項目頁面比較多時,初始化時候加載所有頁面路由,性能十分差,這時候就可用懶加載,要渲染那個頁面就加載那個頁面。
例如:
{
path: '/ChangeCommunity',
name: 'ChangeCommunity',
component: ChangeCommunity,
resolve
},
還可以
{
path: '/ChangeCommunity',
name: 'ChangeCommunity',
component: resolve=>require(['ChangeCommunity'],resolve)
},
兩種寫法都可以。
三 、滾動行為
使用前端路由,當(dāng)切換到新路由時,想要頁面滾到頂部,或者是保持原先的滾動位置,就像重新加載頁面那樣。
vue-router 能做到,而且更好,它讓你可以自定義路由切換時頁面如何滾動。
注意:這個功能只在支持 history.pushState 的瀏覽器中可用。
例如:
const router = new VueRouter({
routes: [...],
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition//滾動到指定位置
} else {
return { x: 0, y: 0 }
}
} })
“滾動到錨點”的行為:
scrollBehavior (to, from, savedPosition) {
if (to.hash) {
return {
selector: to.hash
}
}
}
異步滾動
scrollBehavior (to, from, savedPosition) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ x: 0, y: 0 })
}, 500)
})
}
以上這篇Vue兩種組件類型:遞歸組件和動態(tài)組件的用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vue3的動態(tài)組件是如何工作的
- 深入了解Vue動態(tài)組件和異步組件
- vue 動態(tài)組件(component :is) 和 dom元素限制(is)用法說明
- vue 動態(tài)組件用法示例小結(jié)
- vue學(xué)習(xí)筆記之動態(tài)組件和v-once指令簡單示例
- VUE 動態(tài)組件的應(yīng)用案例分析
- Vue 動態(tài)組件components和v-once指令的實現(xiàn)
- Vue動態(tài)組件和異步組件原理詳解
- vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼
- Vue動態(tài)組件與異步組件實例詳解
- vue使用動態(tài)組件實現(xiàn)TAB切換效果
相關(guān)文章
vuex新手進(jìn)階篇之a(chǎn)ctions的使用方法
actions用來處理mutations中的異步操作,觸發(fā)mutations中的函數(shù),下面這篇文章主要給大家介紹了關(guān)于vuex新手進(jìn)階篇之a(chǎn)ctions的使用方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
前端在el-dialog中嵌套多個el-dialog代碼實現(xiàn)
最近使用vue+elementUI做項目,使用過程中很多地方會用到dialog這個組件,有好幾個地方用到了dialog的嵌套,下面這篇文章主要給大家介紹了關(guān)于前端在el-dialog中嵌套多個el-dialog代碼實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01
vue+elementui實現(xiàn)動態(tài)控制表格列的顯示和隱藏
這篇文章主要介紹了vue+elementui實現(xiàn)動態(tài)控制表格列的顯示和隱藏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

