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

/ 5,958评论 / 26596阅读 / 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. Lhaneuncog说道:

    Hi this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be greatly appreciated!
    best online casino Australia

  2. Derekjup说道:

    amoxicillin from canada: amoxil – amoxicillin 500 mg without a prescription

  3. StevenWearo说道:

    priligy: max pharm – priligy maxpharm

  4. This is a really good tip especially to those new to the blogosphere. Brief but very precise info… Thank you for sharing this one. A must read article!

  5. StevenWearo说道:

    priligy max pharm: buy dapoxetine online – dapoxetine online

  6. Brandenven说道:

    buy prednisone tablets online: ray pharm – prednisone prices

  7. GLYCOGEN PLUS说道:

    Loved this entry. It’s highly comprehensive and filled with useful information. Great work!

  8. SUGAR DEFENDER说道:

    This is truly fantastic. I thought the information highly helpful and articulate. Thanks for providing such helpful information.

  9. Brandenven说道:

    buy amoxicillin online uk: Com Pharm – amoxicillin 500mg pill

  10. StevenWearo说道:

    cost of clomid prices: clomid online – where to buy cheap clomid without prescription

  11. Derekjup说道:

    Priligy tablets: priligy – Priligy tablets

  12. StevenWearo说道:

    cost of clomid: rex pharm – can i purchase clomid without insurance

  13. Brandenven说道:

    amoxicillin 500 mg tablet: amoxil com pharm – order amoxicillin online no prescription

  14. Iyannanet说道:

    fortune mouse gratis
    fortune mouse gratis слот

  15. Brandenven说道:

    can i buy generic clomid no prescription: generic clomid – where buy generic clomid without rx

  16. CurtisWer说道:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  17. go right here说道:

    Way cool! Some extremely valid points! I appreciate you writing this post and the rest of the website is really good.

  18. StevenWearo说道:

    buying generic clomid price: buy clomid – cost generic clomid without a prescription

  19. Truly appreciated reading this article. It’s very informative and well-organized. Fantastic effort!

  20. This post is wonderful. I gained tons from going through it. The details is extremely educational and structured.

  21. Iyannanet说道:

    fortune mouse gratis
    fortune mouse gratis слот

  22. StevenWearo说道:

    where can i get prednisone over the counter: ray pharm – prednisone in mexico

  23. Iyannanet说道:

    игровой аппарат fortune mouse gratis
    fortune mouse gratis на деньги

  24. Brandenven说道:

    prescription for amoxicillin: buy amoxil online – buy amoxicillin online mexico

  25. Derekjup说道:

    amoxicillin 500mg for sale uk: buying amoxicillin in mexico – ampicillin amoxicillin

  26. This article is fantastic. I picked up plenty from perusing it. The details is highly enlightening and structured.

  27. Узнай все о варикоцеле 3 степени клиника варикоцеле

  28. Excellent post. I discovered the content extremely helpful. Adored the way you explained all the points.

  29. web site说道:

    web site best social media services.

  30. Brandenven说道:

    dapoxetine online: priligy – dapoxetine online

  31. Узнай все о заболевание варикоцеле варикоцеле

  32. Derekjup说道:

    dapoxetine online: buy dapoxetine online – priligy maxpharm

  33. CurtisWer说道:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  34. StevenWearo说道:

    buy priligy max pharm: dapoxetine online – priligy max pharm

  35. Hello there! I just want to give you a big thumbs up for your great information you have got right here on this post. I will be coming back to your blog for more soon.

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

  37. Terrific article. It’s extremely well-written and full of beneficial information. Thank you for providing this content.

  38. Тут можно преобрести сейфов для оружия шкаф оружейный

  39. StevenWearo说道:

    Priligy tablets: buy dapoxetine online – cheap priligy

  40. DanielArill说道:

    buy amoxicillin online no prescription http://clomidrexpharm.com/# where to get cheap clomid prices

  41. DanielArill说道:

    buy cheap amoxicillin online https://amoxilcompharm.com/# buy amoxicillin 500mg online

  42. Brandenven说道:

    prednisone 25mg from canada: prednisoneraypharm – prednisone uk price

发表回复

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