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

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

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

組件間傳值的兩個(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)文章

最新評(píng)論