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

/ 5,955评论 / 26581阅读 / 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. porn说道:

    I needed to thank you for this fantastic read!! I certainly enjoyed every little bit of it. I have got you book marked to look at new stuff you post…

  2. StevenWearo说道:

    prednisone 2 5 mg: buy prednisone – can i buy prednisone online without a prescription

  3. Brandenven说道:

    priligy: buy priligy – cheap priligy

  4. Derekjup说道:

    how much is prednisone 5mg: raypharm – prednisone pill 10 mg

  5. Iyannanet说道:

    jack poker отзывы игроков
    jack poker отзывы онлайн

  6. Brandenven说道:

    prednisone 2 5 mg: ray pharm – order prednisone

发表回复

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