【从0到项目实战 vue3+vite】7. vue3基础v-model数据绑定


开始

还原App.vue为如下,以下操作都在App.vue

<script setup>

</script>

<template>
  
</template>

<style scoped>

</style>

运行项目

yarn dev

在这里插入图片描述

浏览器打开http://localhost:5173/
在这里插入图片描述
为一片空白,则为下面的教程做准备




数据双向绑定

基础绑定checkbox

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const checked = ref(false)

</script>

<template>
  <input type="checkbox" id="checkbox" v-model="checked" />
  <label for="checkbox">{{ checked }}</label>
</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示
    1.
    在这里插入图片描述
    2.
    在这里插入图片描述

基础绑定input

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const text = ref('')

</script>

<template>
  <p>text is: {{ text }}</p>
  <input v-model="text" placeholder="修改" />
</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示

在这里插入图片描述

基础绑定复选框

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const checkedNames = ref([])

</script>

<template>
  <div>Checked names: {{ checkedNames }}</div>

  <input type="checkbox" id="value1" value="value1" v-model="checkedNames">
  <label for="value3">value1</label>

  <input type="checkbox" id="value2" value="value2" v-model="checkedNames">
  <label for="value2">value2</label>

  <input type="checkbox" id="value3" value="value3" v-model="checkedNames">
  <label for="value3">value3</label>
</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示
    1.
    在这里插入图片描述
    2.
    在这里插入图片描述

基础绑定radio

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const picked = ref("One")

</script>

<template>
  <div>Picked: {{ picked }}</div>

  <input type="radio" id="one" value="One" v-model="picked" />
  <label for="one">One</label>

  <input type="radio" id="two" value="Two" v-model="picked" />
  <label for="two">Two</label>

</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示
    1.
    在这里插入图片描述
    2.
    在这里插入图片描述

基础绑定select-option

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const selected = ref("A")

</script>

<template>
  <div>Selected: {{ selected }}</div>

  <select v-model="selected">
    <option disabled value="">Please select one</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
  </select>

</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示

true-value和false-value

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const toggle = ref('')

</script>

<template>
  <div>toggle: {{ toggle }}</div>
  <input
    type="checkbox"
    v-model="toggle"
    true-value="yes"
    false-value="no" />

</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示

在这里插入图片描述
在这里插入图片描述

数字类型

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const pick = ref('')

</script>

<template>
  <div>pick: {{ pick }}; type: {{ typeof pick }}</div>
  <input type="radio" v-model="pick" :value="1" />
  <input type="radio" v-model="pick" :value="2" />


</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示

在这里插入图片描述

混合类型

  • 完整代码
<script setup>
import { ref,computed } from 'vue';

const pick = ref('')

</script>

<template>
  <div>pick: {{ pick }}; type: {{ typeof pick }}</div>
  <select v-model="pick">
    <option :value="{ number: 123 }">123</option>
    <option :value="234">234</option>
  </select>
</template>

<style scoped>
input{
  font-size: 20px;
}
</style>
  • 显示
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3 + TypeScript + Vite 中使用 vue-draggable-next 插件实现按钮在区域内任意移动的功能,你可以按照以下步骤进行操作: 1. 确保你已经安装了 Vue 3、TypeScript 和 Vite,并且已经创建了一个 Vue 3 + TypeScript + Vite 的项目。 2. 在项目根目录下,通过命令行安装 vue-draggable-next: ```bash npm install vue-draggable-next ``` 3. 在你的 Vue 组件中,首先导入所需的库和类型: ```typescript import { defineComponent, ref } from 'vue'; import { draggable } from 'vue-draggable-next'; ``` 4. 创建一个包含按钮的区域容器,并在容器内部使用 `draggable` 组件来包裹按钮。将 `v-model` 指令绑定到按钮的位置属性。 ```typescript export default defineComponent({ components: { draggable }, setup() { const buttonPosition = ref({ x: 0, y: 0 }); return { buttonPosition }; } }); ``` 5. 在模板中使用 `draggable` 组件和按钮。 ```html <template> <div class="container"> <draggable v-model="buttonPosition" :bounds="containerBounds"> <button class="button">移动按钮</button> </draggable> </div> </template> ``` 6. 添加样式,确保容器具有适当的宽度、高度和边界。 ```html <style> .container { position: relative; width: 500px; height: 500px; border: 1px solid #ccc; } .button { position: absolute; left: 0; top: 0; } </style> ``` 通过以上步骤,你就可以在 Vue 3 + TypeScript + Vite 中使用 vue-draggable-next 插件来实现按钮在区域内任意移动的功能。这样,按钮的位置将随着拖动而改变,并受到容器边界的限制。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半调子全栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值