vue3之v-model之helloworld
v-model在3.4升級了,舊的v-model是綁定到一個ref上,新的v-model是經過一個defineModel()。
看半天沒看懂,繼續用舊的v-model。
下面的代碼,替換到vue默認生成的components/HelloWorld.vue里就行。
<script setup lang="ts">
import {ref} from 'vue'
defineProps<{ msg: string}>()
const userName = ref("")
</script>
<template>
<div class="greetings">
<h1 class="green">{{ msg }}</h1>
<input type="text" v-model="userName"></input>
<input type="text" v-model="userName"></input>
</div>
</template>
<style scoped>
h1 {
font-weight: 500;
font-size: 2.6rem;
position: relative;
top: -10px;
}
實現的時在一個輸入框輸入,另一個輸入框同步回顯的效果。
其中import {ref} from 'vue'時必須的,不然報錯Cannot find name 'ref'.

浙公網安備 33010602011771號