当前位置: 首页 > news >正文

008 前端vue

vue优点之一:元素和数据能同时变化

一、vue2

1.基本框架

基本写法:

<template><div></div></template>
<script lang="ts">
// 以下是vue2的写法
export default{// 所有数据放在data里面data() {return{}},//所有函数都放在这里methods:{}
}
</script>
<style scoped></style>

2.表单控件获取值

3.表单控件获取文本信息

4.绑定事件

5.单选按钮

6.选择多个选项(数组)

7.信息成组出现

8.遍历多个值

9.总汇

10.追加

添加样式和css一样

11.v-if

是否有

12.v-show

是否展示

13.全部代码

<template><div><!-- 单选/多选/下拉框这种表单控件,要获取值,用v-model --><!-- br表示插入一个换行符 --><!-- 获取文本信息用两个花括号 -->姓名:<input type="text" v-model="username" >{{ username }}<br>成绩:<input type="text" v-model="score">{{ score }}<br><!-- 设置按钮成绩提高 --><!-- 给button绑事件,点击之后触发添加成绩的方法 addScore()--><button v-on:click="addScore()" >成绩提高</button><div><!-- <input type="radio"> 表示单选按钮,用于从一组选项中选择唯一的一个选项 --><!-- 想让对象成组出现,v-model="对象名+属性名" -->性别:<input type="radio" value="1" v-model="userInfo.sex">男<input type="radio" value="2" v-model="userInfo.sex">女<br>年龄:<input type="number" v-model="userInfo.age"><br><!-- 多个选项选一个 -->学科:<select v-model="userInfo.obj"><option value="Math">数学</option><option value="Chinese">语文</option><option value="English">英语</option></select><br><!-- 数组用循环实现成组出现 -->爱好:<span v-for="(skill, index) in userInfo.interest" :key="index">{{ skill }}</span><!-- 设置添加选项按钮,点击这个按钮触发新增爱好方法,并且绑定新爱好这个输入框 -->新爱好:<input type="text" v-model="newInterest"><button v-on:click="addInterest()">增加新爱好</button><!-- 总汇 -->个人信息<div>{{ userInfo }}</div></div></div>
</template><script lang="ts">
// 以下是vue2的写法
export default{// 所有数据放在data里面data() {return{username:"张三",score:500,userInfo:{sex:1,age:23,obj:"Math",// 爱好可以有很多个,所以可以用数组interest:['篮球','足球','乒乓球']},// 新增爱好,初始值为空newInterest:""}},//所有函数都放在这里methods:{addScore(){this.score+=10},// 添加方法:新增爱好addInterest(){// 如果不是空的电话,执行下面语句if(this.newInterest)// 增加this.userInfo.interest.push(this.newInterest)}}
}
</script>
<style scoped></style>

二、vue3

只能在控制台打印

<template><div><!-- 单选/多选/下拉框这种表单控件,要获取值,用v-model --><!-- br表示插入一个换行符 --><!-- 获取文本信息用两个花括号 -->姓名:<input type="text" v-model="username" >{{ username }}<br>成绩:<input type="text" v-model="score">{{ score }}<br><!-- 设置按钮成绩提高 --><!-- 给button绑事件,点击之后触发添加成绩的方法 addScore()--><button v-on:click="addScore()" >成绩提高</button></div></template>
<script lang="ts">export default{setup() {let username="李华"let score =500function addScore(){console.log(score)score+=20}return{username,score,addScore}},}</script>
<style scoped></style>

1.双向绑定

ref函数(自动导入)

2.封装

<template><div><!-- 单选/多选/下拉框这种表单控件,要获取值,用v-model --><!-- br表示插入一个换行符 --><!-- 获取文本信息用两个花括号 -->姓名:<input type="text" v-model="username" >{{ username }}<br>成绩:<input type="text" v-model="score">{{ score }}<br><!-- 设置按钮成绩提高 --><!-- 给button绑事件,点击之后触发添加成绩的方法 addScore()--><button v-on:click="addScore()" >成绩提高</button></div></template>
<script lang="ts">
import { ref } from 'vue'export default{setup() {let username=ref("李华")let score =ref(500)function addScore(){console.log(score)score.value +=20}return{username,score,addScore}},}</script>
<style scoped></style>
<template><div>姓名:<input type="text" v-model="userInfo.username">{{ userInfo.username }}<br>成绩:<input type="text" v-model="userInfo.score">{{ userInfo.score }}<br><button v-on:click="addScore">成绩提高</button></div>
</template><script lang="ts">
import { reactive } from 'vue';export default {setup() {// 用reactive创建响应式对象管理用户信息const userInfo = reactive({username: "李华",score: 500});// 定义事件处理函数,直接访问userInfofunction addScore() {console.log(userInfo.score);userInfo.score += 20;}return { userInfo, addScore };}
};
</script><style scoped></style>

简洁写法

http://www.lryc.cn/news/613201.html

相关文章:

  • 解决React白板应用中的画布内容丢失问题
  • [盛最多水的容器]
  • 【关于Java中==和equals( )和hashCode( )三者异同】
  • Java中接口与抽象类
  • 国内使用 npm 时配置镜像源
  • 2025年 IT 服务管理(ITSM)工具市场分析:选型逻辑与企业适配趋势报告
  • Spring Cloud系列—LoadBalance负载均衡
  • 边缘算力×AI应用:如何在2025年实现爆发式增长
  • 酉矩阵(Unitary Matrix)和随机矩阵
  • “认知裂缝边缘”地带
  • PyTorch LSTM文本生成
  • 基于深度学习的污水新冠RNA测序数据分析系统
  • 进程Linux
  • TSMaster-C小程序使用
  • 深度学习之opencv篇
  • change和watch
  • GPT-5 将在周五凌晨1点正式发布,王炸模型将免费使用??
  • 16.Home-懒加载指令优化
  • [C++20]协程:语义、调度与异步 | Reactor 模式
  • 在 Linux 系统上安装 Docker 的步骤如下(以 Ubuntu/Debian为例)
  • 深度学习(1):pytorch
  • Android-Kotlin基础(Jetpack②-Data Binding)
  • 内存杀手机器:TensorFlow Lite + Spring Boot移动端模型服务深度优化方案
  • Bosco-and-Mancuso Filter for CFA Image Denoising
  • python函数--python010
  • Java NIO 核心原理与秋招高频面试题解析
  • MySQL 极简安装挑战:跨平台高效部署指南
  • 大数据中需要知道的监控页面端口号都有哪些
  • 【unity知识】unity使用AABB(轴对齐包围盒)和OBB(定向包围盒)优化碰撞检测
  • 单词的划分(动态规划)