[vue3]组件间v-bind,v-model传参(defineProps,defineEmits)

/ 6,002评论 / 26748阅读 / 2点赞

1. 自定义组件间也可以和普通标签(<div> <span>)一样指定属性传参

2. 形如(<child value="hello coolight"></child>)给组件<child>指定value属性为"hello coolight"

3. 我们也可以自己模拟v-model的效果

下面我们来聊聊怎么传参、接收与更新

传参,v-bind

<script setup>
import { toRefs } from 'vue';
const props = defineProps({
    text:{
        type:String,
        default:"默认:hello coolight",
        required:false
    }
})

const {
    text,
} = toRefs(props);
</script>

<template>
    <span>{{props.text}} - {{text}}</span>
</template>
<script setup>
import helloVue from '../../components/global/hello.vue';
</script>

<template>
    <div>
        <hello-vue />
        <hello-vue text="洛天依"/>
        <hello-vue :text="'v-bind:洛天依'" />
    </div>
</template>

v-model

一般的传参进来,组件对它是只读的,不能修改。

只读也并不完全,对于数组和对象是可以操作其内部值的。

但我们可以使用v-model指定参数,使之允许组件来修改它。

”只读“的探讨

<script setup>
import { reactive } from 'vue';
import { toRefs } from 'vue';
const props = defineProps({
    obj:{
        type:Object
    }
})

const {
    obj,
} = toRefs(props);

const btn1_click = () => {
    props.obj.hello = "coolight - 1";
}
const btn2_click = () => {
    props.obj = reactive({
        hello:"coolight - 2"
    })
}
</script>

<template>
    <div>
        <button @click="btn1_click">1: {{obj.hello}}</button>
        <button @click="btn2_click">2: {{obj.hello}}</button>
    </div>
</template>
<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';

let obj = reactive({
    hello:"洛天依"
})
</script>

<template>
    <div>
        <span>span: {{obj.hello}}</span>
        <hello-vue :obj="obj" />
    </div>
</template>

使用v-model

<script setup>
import { toRefs } from 'vue';
const props = defineProps({
    obj:{
        type:Object
    }
})
const emits =  defineEmits ({
    'update:obj':null  //null位置可以是一个检查函数,用以检查绑定这个信号的处理函数是否有接收参数等,这里就不需要了
});

const {
    obj,
} = toRefs(props);

const btn1_click = () => {
    props.obj.hello = "btn1";
}
const btn2_click = () => {
    emits('update:obj', {
        hello:"btn2"
    });
}
</script>

<template>
    <div>
        <button @click="btn1_click">{{obj.hello}} - 修改为 btn1</button>
        <button @click="btn2_click">{{obj.hello}} - 修改为 btn2</button>
    </div>
</template>
<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';

let mValue = reactive({
    obj:{
        hello:"洛天依"
    },
})
</script>

<template>
    <div>
        <span>span: {{mValue.obj.hello}}</span>
        <hello-vue v-model:obj="mValue.obj"/>
    </div>
</template>

尝试取代v-model

<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';

let mValue = reactive({
    obj:{
        hello:"洛天依"
    },
})
const change = (in_obj) => {
    console.log("我们的change");
    mValue.obj = in_obj;
}
</script>

<template>
    <div>
        <span>span: {{mValue.obj.hello}}</span>
        <!-- 也可以这样:
            <hello-vue v-model:obj="mValue.obj" @update:obj="change"/>
        -->
        <hello-vue :obj="mValue.obj" @update:obj="change"/>
    </div>
</template>
  1. Dereknef说道:

    Canl? Casino Siteleri: guvenilir casino siteleri – guvenilir casino siteleri

  2. LarryBow说道:

    en kazancl? slot oyunlar? slot siteleri slot oyunlar?

  3. ClydeTal说道:

    ultrabet giris: ultrabet guncel – ultrabet guncel

  4. CarrollWip说道:

    deneme bonusu veren siteler betturkey https://denemebonusuverensiteler.top/# deneme bonusu veren siteler

  5. Dereknef说道:

    Casino Siteleri: Canl? Casino Siteleri – Deneme Bonusu Veren Siteler

  6. ClydeTal说道:

    matadorbet giris: matadorbet – matadorbet

  7. Dereknef说道:

    Casino Siteleri: guvenilir casino siteleri – Casino Siteleri

  8. tubidy说道:

    That is a good tip especially to those fresh to the blogosphere. Short but very precise information… Many thanks for sharing this one. A must read post.

  9. CarrollWip说道:

    deneme bonusu veren siteler betturkey http://casinositeleri.win/# Deneme Bonusu Veren Siteler

  10. LarryBow说道:

    matadorbet.bid matadorbet bid matadorbet

  11. Тут можно преобрести сейф для оружия цена сейфы оружейные

  12. Dereknef说道:

    slot siteleri: az parayla cok kazandiran slot oyunlar? – slot tr online

  13. Dereknef说道:

    ultrabet giris: ultrabet – ultrabet bonus

  14. tubidy说道:

    There is certainly a lot to find out about this topic. I like all of the points you’ve made.

  15. ClydeTal说道:

    slot tr online: slot oyunlar? – en cok kazand?ran slot oyunlar?

  16. CarrollWip说道:

    deneme bonusu veren siteler denemebonusu2026.com https://denemebonusuverensiteler.top/# deneme bonusu veren yeni siteler

  17. ClydeTal说道:

    matadorbet bid: matadorbet – matadorbet bid

  18. LarryBow说道:

    casino siteleri win Casino Siteleri Casino Siteleri

  19. Тут можно преобрести сейф для сайги 12 где купить оружейный сейф

  20. Dereknef说道:

    ultrabet: ultrabet guncel – ultrabet guncel

  21. Dereknef说道:

    Deneme Bonusu Veren Siteler: casino siteleri win – Casino Siteleri

  22. Тут можно преобрести купить сейф для охотничьего ружья в москве стоимость сейфа для оружия

  23. CarrollWip说道:

    deneme bonusu veren siteler yerliarama.org https://casinositeleri.win/# Casino Siteleri

  24. Тут можно преобрести сейфы для оружия цены сейфы оружейные цена

  25. LarryBow说道:

    matadorbet matadorbet bid matadorbet

  26. Very good article. I am experiencing a few of these issues as well..

  27. tubidy说道:

    Great info. Lucky me I came across your blog by chance (stumbleupon). I have bookmarked it for later!

  28. ClydeTal说道:

    casino siteleri win: casino siteleri win – Deneme Bonusu Veren Siteler

  29. ClydeTal说道:

    deneme bonusu veren siteler 2024: deneme bonusu veren siteler mycbet.com – deneme bonusu veren siteler yeni

  30. Dereknef说道:

    slot siteleri: en kazancl? slot oyunlar? – en cok kazand?ran slot oyunlar?

  31. Great post. I found the details extremely beneficial. Appreciated the manner you explained all the points.

  32. Тут можно преобрести купить огнеупорный сейф сейф огнестойкий

  33. CarrollWip说道:

    deneme bonusu veren yeni siteler https://casinositeleri.win/# casino siteleri win

  34. HassanMesty说道:

    https://slot-tr.online/# slot siteleri
    deneme bonusu veren siteler betturkey

  35. Dereknef说道:

    casino siteleri win: Canl? Casino Siteleri – casino siteleri win

  36. This post is amazing! Filled with useful insights and extremely well-written. Many thanks for sharing this.

  37. It’s hard to find well-informed people about this subject, however, you sound like you know what you’re talking about! Thanks

  38. LarryBow说道:

    slot oyunlar? az parayla cok kazandiran slot oyunlar? slot tr online

  39. Happyo说道:

    This article is fantastic. I gained a lot from going through it. The information is extremely educational and well-organized.

  40. kerassentials说道:

    Fantastic entry. I found the details extremely useful. Loved the way you clarified everything.

  41. HassanMesty说道:

    https://denemebonusuverensiteler.top/# deneme bonusu veren siteler betturkey betturkey.com
    deneme bonusu veren siteler yeni

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注