欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3組件間傳值避坑方法指南

 更新時間:2023年03月05日 14:42:45   作者:時光劍客  
這篇文章主要為大家介紹了Vue3組件間傳值避坑方法指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

組件間傳值的兩個坑

我們都知道父組件可以把值傳遞到自組件中,但是有時候子組件需要修改這個父組件傳遞過來的這個值,我們可以想象下能修改成功嗎?這是坑之一。我們在組件間傳值的時候,都是一個屬性名對應(yīng)一個值,接收的時候也是用這個屬性名接收,那么每一個用戶自定義的屬性名都能被接收到嗎?這是坑之二,本文就讓我們一起來看下這兩個坑吧。

實(shí)例填坑

坑一

1. 發(fā)現(xiàn)天坑

我們通過一個計(jì)數(shù)器組件來演示這個坑,當(dāng)想對父組件傳遞過來的值做操作時,發(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>
                ` 
    });
    // 定義一個test組件
    app.component('counter',{
       props: ['count'],
      template: `<div @click="count+=1">{{count}}</div>`
    });
    const vm = app.mount('#root');
</script>
</html>

在上面的代碼中,我們定義了一個counter組件接收父組件的一個count值,當(dāng)點(diǎn)擊這個顯示的值時,我們做加一操作。這時候我們運(yùn)行代碼會發(fā)現(xiàn),我們的值并不會完成加一操作,而是會報(bào)父組件傳遞過來的值是只讀的:

2. 填坑時刻

那假如我們要完成這個加一的功能怎么辦呢?答案就是我們復(fù)制一份父組件傳遞過來的值,對我們自己的值進(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>
                ` 
    });
    // 定義一個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>

這時候我們再運(yùn)行代碼就會發(fā)現(xiàn)我們可以做加一操作了:

坑2:

1.發(fā)現(xiàn)天坑

當(dāng)我們定義一個單詞名稱比較長的屬性,并且用“-”分隔符連接的時候,子組件無法接收到正確的值,顯示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>
                ` 
    });
    // 定義一個test組件
    app.component('test',{
       props: ['content-helloworld'],
      template: `<div>{{content-helloworld}}</div>`
    });
    const vm = app.mount('#root');
</script>
</html>

在上面的代碼中,我們使用content-helloworld這個屬性在父組件和子組件之間傳值,按照我們的理解,應(yīng)該是能傳遞成功的,但是顯示的結(jié)果卻不正確

上面到坑也是VUE中的單向數(shù)據(jù)流的概念,即子組件可以使用父組件傳遞過來的數(shù)據(jù),但是不能修改父組件傳遞過來的數(shù)據(jù)

2.填坑時刻

當(dāng)我們定義的屬性值中有用“-”分隔符分隔時,我們在接收值的時候,需要將屬性名改成駝峰命名的方式,如上面的例子中父組件使用content-helloworld傳遞值到子組件,那么子組件接收到時候應(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>
                ` 
    });
    // 定義一個test組件
    app.component('test',{
       props: ['contentHelloworld'],
      template: `<div>{{contentHelloworld}}</div>`
    });
    const vm = app.mount('#root');
</script>
</html>

這樣值就能正確顯示了

總結(jié)

本文主要是講解了組件傳值過程中的兩個容易犯的小錯誤,一是父組件傳遞過來的值不能修改,二是父組件使用“-”分隔符定義屬性傳遞值到子組件,子組件接收時需要將屬性名改為駝峰命名方式,更多關(guān)于Vue3組件傳值避坑的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論