Vue3組件間傳值避坑方法指南
組件間傳值的兩個(gè)坑
我們都知道父組件可以把值傳遞到自組件中,但是有時(shí)候子組件需要修改這個(gè)父組件傳遞過來的這個(gè)值,我們可以想象下能修改成功嗎?這是坑之一。我們?cè)诮M件間傳值的時(shí)候,都是一個(gè)屬性名對(duì)應(yīng)一個(gè)值,接收的時(shí)候也是用這個(gè)屬性名接收,那么每一個(gè)用戶自定義的屬性名都能被接收到嗎?這是坑之二,本文就讓我們一起來看下這兩個(gè)坑吧。
實(shí)例填坑
坑一
1. 發(fā)現(xiàn)天坑
我們通過一個(gè)計(jì)數(shù)器組件來演示這個(gè)坑,當(dāng)想對(duì)父組件傳遞過來的值做操作時(shí),發(fā)現(xiàn)操作無效,先看代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/vue@next"></script> <title>組件間傳值</title> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data() { return { num:0 } }, template: ` <div> <counter :count = "num"/> </div> ` }); // 定義一個(gè)test組件 app.component('counter',{ props: ['count'], template: `<div @click="count+=1">{{count}}</div>` }); const vm = app.mount('#root'); </script> </html>
在上面的代碼中,我們定義了一個(gè)counter組件接收父組件的一個(gè)count值,當(dāng)點(diǎn)擊這個(gè)顯示的值時(shí),我們做加一操作。這時(shí)候我們運(yùn)行代碼會(huì)發(fā)現(xiàn),我們的值并不會(huì)完成加一操作,而是會(huì)報(bào)父組件傳遞過來的值是只讀的:
2. 填坑時(shí)刻
那假如我們要完成這個(gè)加一的功能怎么辦呢?答案就是我們復(fù)制一份父組件傳遞過來的值,對(duì)我們自己的值進(jìn)行操作:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/vue@next"></script> <title>組件間傳值</title> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data() { return { num:0 } }, template: ` <div> <counter :count = "num"/> </div> ` }); // 定義一個(gè)test組件 app.component('counter',{ props: ['count'], data(){ return{ mCount:this.count } }, template: `<div @click="mCount+=1">{{mCount}}</div>` }); const vm = app.mount('#root'); </script> </html>
這時(shí)候我們?cè)龠\(yùn)行代碼就會(huì)發(fā)現(xiàn)我們可以做加一操作了:
坑2:
1.發(fā)現(xiàn)天坑
當(dāng)我們定義一個(gè)單詞名稱比較長(zhǎng)的屬性,并且用“-”分隔符連接的時(shí)候,子組件無法接收到正確的值,顯示NaN。代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/vue@next"></script> <title>組件間傳值</title> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data() { return { content:"hello world" } }, template: ` <div> <test :content-helloworld = "content"/> </div> ` }); // 定義一個(gè)test組件 app.component('test',{ props: ['content-helloworld'], template: `<div>{{content-helloworld}}</div>` }); const vm = app.mount('#root'); </script> </html>
在上面的代碼中,我們使用content-helloworld
這個(gè)屬性在父組件和子組件之間傳值,按照我們的理解,應(yīng)該是能傳遞成功的,但是顯示的結(jié)果卻不正確
上面到坑也是VUE中的單向數(shù)據(jù)流的概念,即子組件可以使用父組件傳遞過來的數(shù)據(jù),但是不能修改父組件傳遞過來的數(shù)據(jù)
2.填坑時(shí)刻
當(dāng)我們定義的屬性值中有用“-”分隔符分隔時(shí),我們?cè)诮邮罩档臅r(shí)候,需要將屬性名改成駝峰命名的方式,如上面的例子中父組件使用content-helloworld
傳遞值到子組件,那么子組件接收到時(shí)候應(yīng)該將其改成駝峰命名方式:使用contentHelloworld
接收
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/vue@next"></script> <title>組件間傳值</title> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data() { return { content:"hello world" } }, template: ` <div> <test :content-helloworld = "content"/> </div> ` }); // 定義一個(gè)test組件 app.component('test',{ props: ['contentHelloworld'], template: `<div>{{contentHelloworld}}</div>` }); const vm = app.mount('#root'); </script> </html>
這樣值就能正確顯示了
總結(jié)
本文主要是講解了組件傳值過程中的兩個(gè)容易犯的小錯(cuò)誤,一是父組件傳遞過來的值不能修改,二是父組件使用“-”分隔符定義屬性傳遞值到子組件,子組件接收時(shí)需要將屬性名改為駝峰命名方式,更多關(guān)于Vue3組件傳值避坑的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue使用watch監(jiān)聽一個(gè)對(duì)象中的屬性的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue使用watch監(jiān)聽一個(gè)對(duì)象中的屬性的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Vue生命周期activated之返回上一頁不重新請(qǐng)求數(shù)據(jù)操作
這篇文章主要介紹了Vue生命周期activated之返回上一頁不重新請(qǐng)求數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue 2.8.2版本配置剛進(jìn)入時(shí)候的默認(rèn)頁面方法
今天小編就為大家分享一篇vue 2.8.2版本配置剛進(jìn)入時(shí)候的默認(rèn)頁面方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue路由重復(fù)點(diǎn)擊報(bào)錯(cuò)問題及解決
這篇文章主要介紹了Vue路由重復(fù)點(diǎn)擊報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue獲取token實(shí)現(xiàn)token登錄的示例代碼
最近新做了個(gè)vue項(xiàng)目,正好項(xiàng)目中有登錄部分,本文就詳細(xì)的介紹一下登錄部分的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-11-11