亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue手寫(xiě)<RouterLink/>組件實(shí)現(xiàn)demo詳解

 更新時(shí)間:2023年06月16日 09:34:09   作者:XiaoSong  
這篇文章主要為大家介紹了vue手寫(xiě)<RouterLink/>組件實(shí)現(xiàn)demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

手寫(xiě)<RouterLink/>組件

上一節(jié) 手寫(xiě)<RouterView/>組件

如果使用a標(biāo)簽改變頁(yè)面會(huì)重新發(fā)起請(qǐng)求,但是我們點(diǎn)擊<RouterLink/>的時(shí)候不會(huì)。以下手把手實(shí)現(xiàn)一個(gè)簡(jiǎn)單的<RouterLink/>組件。這節(jié)內(nèi)容與手寫(xiě)<RouterView/>組件(上一節(jié)內(nèi)容)有一定的聯(lián)系,建議先看一下。

1、加上一個(gè)響應(yīng)式數(shù)據(jù)

在上一節(jié)的基礎(chǔ)上加上一個(gè)響應(yīng)式數(shù)據(jù)(把獲取當(dāng)前路徑寫(xiě)在了這里)。

import?About?from?'../view/About.vue'
import?Home?from?'../view/Home.vue'
import?{ref}?from?'vue'
export?default?[
????{
????????path:?'/',
????????component:?Home
????},
????{
????????path:?'/about',
????????component:?About
????}
]
//?新加內(nèi)容
export?const?path?=?ref(window.location.pathname)

2、對(duì)<RouterView/>組件進(jìn)行簡(jiǎn)單的改造

<template>
????<div>
????????<component?:is="view"></component>
????</div>
</template>
<script?setup>
import?{?computed?}?from?'vue'
import?Router?from?'../Router';
//?引入path這個(gè)響應(yīng)式數(shù)據(jù)
import?{?path?}?from?'../Router';
const?view?=?computed(()?=>?{
??//?這里直接使用的path
???const?res?=?Router.find(item?=>?item.path?==?path.value)
???return?res.component
})
</script>
<style?lang="scss"?scoped>
</style>

3、創(chuàng)建<RouterLink/>組件

內(nèi)容如下:

@click.prevent阻止a標(biāo)簽的默認(rèn)行為,讓其被點(diǎn)擊時(shí)執(zhí)行push函數(shù)。push函數(shù)里執(zhí)行的就是改變path為要加載的頁(yè)面路徑。

<template>
????<div>
????????<a?:href="to" rel="external nofollow" ?@click.prevent="push">
????????????<slot></slot>
????????</a>
????</div>
</template>
<script?setup>
import?{?path?}?from?'../Router';
const?props?=?defineProps({
????to:?{type:?String,?required:?true}
})
const?push?=?()?=>?{
????path.value?=?props.to
}
</script>

4、在App.vue中使用

當(dāng)你點(diǎn)擊時(shí)不會(huì)重新請(qǐng)求。

<RouterLink?:to="'/'">首頁(yè)</RouterLink>
<RouterLink?:to="'/about'">關(guān)于</RouterLink>
<RouterView></RouterView>

以上就是vue手寫(xiě)RouterLink組件實(shí)現(xiàn)demo詳解的詳細(xì)內(nèi)容,更多關(guān)于vue手寫(xiě)RouterLink組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論