最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3?中v-model語法糖示例詳解

 更新時間:2024年06月25日 14:51:49   作者:寶子卡粉  
vue3中的v-model相當于vue2中的v-model和v-bind.sync 修飾符組合在一起的產(chǎn)物(擇優(yōu)整合)v-bind.sync 在 vue3 中被移除了可以在組件標簽上使用多個 v-model 綁定屬性,使用參數(shù)區(qū)分,這篇文章主要介紹了vue3?中v-model語法糖,需要的朋友可以參考下

一、 vue2 中 v-model 語法糖

實現(xiàn)父子組件雙向數(shù)據(jù)綁定,一個輸入框或者組件指定綁定一個 v-model

1. 父組件寫法

<template>
  <div>
    <h1>App</h1>
    <h2>{{ count }}</h2>
    <input type="text" v-model="count" />
    <!-- 展開寫法,@input中的 count 的值來自當前輸入框事件 -->
    <input type="text" :value="count" @input="count = $event.target.value" />
    <hr />
    <Children v-model="count"></Children>
    <!-- 展開寫法,@input中的 count 的值來自子組件輸入框中的值 $event.target.value -->
    <Children :value="count" @input="count = $event"></Children>
  </div>
</template>
<script>
import Children from "@/components/Children.vue";
export default {
  components: {
    Children,
  },
  data() {
    return {
      count: "1",
    };
  },
};
</script>

2. 子組件寫法

<template>
  <div>
    <h1>main</h1>
    <div>{{ value }}</div>
    <input
      type="text"
      :value="value"
      @input="$emit('input', $event.target.value)"
    />
  </div>
</template>
<script>
export default {
  props: {
    value: {
      type: String,
    },
  },
};
</script>

二、 vue2 的 v-bind.sync 修飾符語法糖,實現(xiàn)父子組件雙向數(shù)據(jù)綁定

注意:子組件標簽中可以同時使用多個 .sync 修飾符

1. 在父組件中

<template>
  <div>
    <h1>App</h1>
    <input v-model="count" type="text" />
    <hr />
    <Children :count.sync="count"></Children>
    <!-- 展開寫法,@update:value中的 count 的值來自子組件輸入框中的值 $event.target.value -->
    <Children :count="count" @update:count="count = $event"></Children>
  </div>
</template>
<script>
import Children from "@/components/Children.vue";
export default {
  components: {
    Children,
  },
  data() {
    return {
      count: "1",
    };
  },
};
</script>

2. 在子組件中

<template>
  <div>
    <h1>main</h1>
    <h2>{{ count }}</h2>
    <input
      type="text"
      :value="count"
      @input="$emit('update:count', $event.target.value)"
    />
  </div>
</template>
<script>
export default {
  props: {
    count: {
      type: String,
    },
  },
};
</script>

三、 vue3 的 v-model 語法糖

vue3 中的 v-model 相當于 vue2 中的 v-model 和 v-bind.sync 修飾符組合在一起的產(chǎn)物(擇優(yōu)整合)v-bind.sync 在 vue3 中被移除了可以在組件標簽上使用多個 v-model 綁定屬性,使用參數(shù)區(qū)分

1. 在父組件中

<template>
  <div>
    <h1>App</h1>
    <h2>{{ count }}</h2>
    <input type="text" v-model="count" />
    <!-- 此展開寫法,僅限于輸入框 -->
    <input type="text" :value="count" @input="count = $event.target.value" />
    <hr />
    <Children v-model:count="count"></Children>
    <!-- 此展開寫法,僅限于組件 -->
    <Children :count="count" @update:count="count = $event"></Children>
  </div>
</template>
<script>
import Children from "@/components/Children.vue";
import { ref } from "vue";
export default {
  components: {
    Children,
  },
  setup() {
    const count = ref("1");
    return { count };
  },
};
</script>

2. 在子組件中

<template>
  <div>
    <h1>main</h1>
    <div>{{ count }}</div>
    <label>
      count:<input
        type="text"
        :value="count"
        @input="emit('update:count', $event.target.value)"
      />
    </label>
  </div>
</template>
<script>
export default {
  props: {
    count: {
      type: String,
    },
  },
  setup(props, { emit }) {
    console.log(props);
    return { emit };
  },
};
</script>

到此這篇關(guān)于vue3 中v-model語法糖的文章就介紹到這了,更多相關(guān)vue3 v-model語法糖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用vscode 開發(fā)uniapp的方法

    使用vscode 開發(fā)uniapp的方法

    本文給大家分享我使用vscode開發(fā)的一些配置。其中包括uniapp組件語法提示,uniapp代碼提示,代碼自動格式化問題,對vscode 開發(fā)uniapp的相關(guān)知識感興趣的朋友一起看看吧
    2021-08-08
  • vue中組件如何使用vue-quill-editor

    vue中組件如何使用vue-quill-editor

    這篇文章主要介紹了vue中組件如何使用vue-quill-editor問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實例

    Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實例

    這篇文章主要介紹了Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作,結(jié)合實例形式分析了vue基于axios庫post傳送表單json格式數(shù)據(jù)相關(guān)操作實現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2023-06-06
  • Vue實現(xiàn)路由跳轉(zhuǎn)的3種方式超詳細分解

    Vue實現(xiàn)路由跳轉(zhuǎn)的3種方式超詳細分解

    Vue.js是一款流行的前端JavaScript框架,它提供了多種方式來實現(xiàn)路由跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)路由跳轉(zhuǎn)的3種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • vue2使用el-tag實現(xiàn)自定義菜單導(dǎo)航標簽

    vue2使用el-tag實現(xiàn)自定義菜單導(dǎo)航標簽

    這篇文章主要為大家詳細介紹了vue2如何使用el-tag實現(xiàn)自定義菜單導(dǎo)航標簽,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Vue ElementUI this.$confirm async await封裝方式

    Vue ElementUI this.$confirm async await封

    這篇文章主要介紹了Vue ElementUI this.$confirm async await封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue子組件獲取到它父組件數(shù)據(jù)的4種方法

    vue子組件獲取到它父組件數(shù)據(jù)的4種方法

    這篇文章主要給大家介紹了關(guān)于vue子組件獲取到它父組件數(shù)據(jù)的4種方法,對于vue來說組件之間的消息傳遞是非常重要的,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-08-08
  • Vue如何導(dǎo)出頁面為word格式

    Vue如何導(dǎo)出頁面為word格式

    這篇文章主要介紹了Vue如何導(dǎo)出頁面為word格式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 深入了解Vue動態(tài)組件和異步組件

    深入了解Vue動態(tài)組件和異步組件

    這篇文章主要介紹了深入了解Vue動態(tài)組件和異步組件的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)vue組件的相關(guān)知識,感興趣的朋友可以了解下
    2021-01-01
  • Vue 不定高展開動效原理詳解

    Vue 不定高展開動效原理詳解

    本文主要介紹了Vue不定高展開動效原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評論

新兴县| 威远县| 乐都县| 汾阳市| 德州市| 靖边县| 姜堰市| 健康| 日喀则市| 德阳市| 三穗县| 朝阳市| 利津县| 邯郸市| 建湖县| 海伦市| 东莞市| 大荔县| 定日县| 岳阳市| 鹤山市| 股票| 新营市| 桃园县| 泰安市| 阆中市| 临夏县| 云浮市| 乐安县| 剑川县| 桃源县| 武清区| 牟定县| 临海市| 大城县| 七台河市| 东明县| 灵丘县| 久治县| 陆良县| 顺义区|