Vue入门1

本文通过实例演示了Vue.js的基本用法,包括数据渲染、双向绑定、条件与循环、数据翻转、事件与数据绑定等关键特性,并展示了如何在实际项目中应用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引入Vue

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

或者

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

2、声明式数据渲染

JS代码:

var app = new Vue({
  el: '#msgDiv',
  data: {
    message: 'Hello word'
  }
})

HTML代码:

<div id="msgDiv">
  {{ message }}
</div>

运行结果:
在这里插入图片描述

3、数据双向绑定 v-model

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
			message:'<h1>Hello word</h1>'
		}
	})

HTML代码:

<div id="msgDiv" >
<p>【文本显示】{{message}}</p>
<p>【html显示】<span v-html="message"/></p>
<p>请输入信息:<input type="text" v-model="message"></input></p>
</div

运行结果:
在这里插入图片描述
补充:
(1)延迟修改 lazy:
即输入完成后再显示出来

<input type="text" v-model.lazy="message"></input>

(2)空格处理 trim:
即输入空格不会显示出来

<input type="text" v-model.trim="message"></input>

4、条件与循环

(1)条件 v-if与v-else-if:

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
			age:'16'
		}
	})

HTML代码:

<div id="msgDiv" >
<p>请输入年龄:<input type='number' v-model='age'/></p>
<p v-if='age>0 && age<18'>小屁孩!!!好好读书</p>
<p v-else-if='age>=18 && age<50'>青年人!!!好好努力奋斗</p>
<p v-else-if='age>=50 && age<250'>老年人!!!好好享受</p>
<p v-else>非人类!!!</p>
</div>

运行结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)循环 v-for:

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
		depts:[
		{'dno':10,'dname':'财务处','loc':'上海'},
		{'dno':20,'dname':'管理处','loc':'深圳'},
		{'dno':30,'dname':'教务处','loc':'北京'}
		]
		}
	})

HTML代码:

<div id="msgDiv" >
<table>
<thead><tr><th>部门编号</th><th>部门名称</th><th>部门位置</th></tr></thead>
<tbody>
<tr v-for='dept in depts'>
<td>{{dept.dno}}</td>
<td>{{dept.dname}}</td>
<td>{{dept.loc}}</td>
</tr>
</tbody>
</table>
</div>

运行结果:
在这里插入图片描述

5、数据翻转

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
			message:'Hello word'
		}
	})

HTML代码:

<div id="msgDiv" >
<h1>【原始消息】:{{message}}</h1>
<h1>【翻转消息】:{{message.split('').reverse().join('')}}</h1>
</div>

运行结果:
在这里插入图片描述

6、绑定处理

(1)事件绑定v-on

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
		message:'努力学习!!!'
		},
		methods:{
		changemessage:function(newMessage){
		//	console.log()表示在控制台显示的内容
		console.log('【原始数据】'+this.message);
		console.log('【更改后数据】'+newMessage);
		this.message=newMessage;
		}
		}
	})

HTML代码:

<div id="msgDiv" >
<p>{{message}}</p>
<button v-on:click="changemessage('争取保研!!!')">修改信息</button>
<!-- <button @click="changemessage('争取保研!!!')">修改信息</button> -->
</div>

运行结果:
在这里插入图片描述
其中v-on:可以写成@
【ES标准】
".stop”:阻止事件冒泡操作;.
“.prevent”:提交事件不进行页面的重新加载;
".self”:只在本元素上进行事件的触发;
“.once”:此事件只执行一次。
如:

<button @click.once="changemessage('争取保研!!!')">修改信息</button>

(2)数据绑定v-bind

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
		       message:'加油学习,努力保研!!!'
		}
	})

HTML代码:

<div id="msgDiv" >
<image src="image/logo5.jpg" v-bind:title="message"></image>
<!-- <image src="image/logo5.jpg" :title="message"></image> -->
</div>

运行结果:
在这里插入图片描述
其中v-bind:可以写成:

(3)数组绑定:

JS代码:

app=new Vue({
		el:'#msgDiv',
		data:{
		id:'depttable',
		trPrefix:'dept-',
		inputdno:'',
		inputdname:'',
		inputloc:'',
		depts:[
		{'dno':10,'dname':'财务处','loc':'上海'},
		{'dno':20,'dname':'管理处','loc':'深圳'},
		{'dno':30,'dname':'教务处','loc':'北京'}
		]
		},
		methods:{
		reverseArray:function(){
		this.depts.reverse();
		},
		addArray:function(){
		this.depts.push({'dno':this.inputdno,'dname':this.inputdname,'loc':this.inputloc});
		this.resetArray(); 
		},
		resetArray:function(){
		this.inputdno='';
		this.inputdname='';
		this.inputloc='';
		},
		deleteArray:function(ind){
		this.depts.splice(ind,1)
		}
		}
	})

HTML代码:

<div id="msgDiv" >
<div id="deptForm" style="float:left;width:30%">
部门编号:<input type="text" v-model="inputdno"/><br/>
部门名称:<input type="text" v-model="inputdname"/><br/>
部门位置:<input type="text" v-model="inputloc"><br/>
<button @click="addArray()">添加数据</button>
<button @click="resetArray()">重置数据</button>
</div>
<div id="deptList" style="float:left;width:50%">
<table :id="id">
<thead><tr><th>索引</th><th>部门编号</th><th>部门名称</th><th>部门位置</th><th>操作</th></tr></thead>
<tbody>
<tr v-for='(dept,index) in depts' :id="trPrefix+dept.dno">
<td>{{index}}</td>
<td>{{dept.dno}}</td>
<td>{{dept.dname}}</td>
<td>{{dept.loc}}</td>
<td><button @click="deleteArray(index)">删除</button></td>
</tr>
</tbody>
</table>
<button @click="reverseArray()">反转数组</button>
<div>
</div>

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值