点赞多大胆,就有多大产!开源促使进步,献给每一位技术使用者和爱好者!
干货满满,摆好姿势,点赞发车
本章节咱们来说一下Vue中常用的指令,不废话直接上干货
根据表达值的真假,切换元素的显示和隐藏
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<img src="./img/xiaozhuang.jpg" v-show="flag">
<span v-show="age >= 18">18岁以上才能看到哦span>
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
flag:true,
age:18
}
})
script>
html>
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<button type="button" @click="showOrHide()">点击隐藏/显示button>
<img src="./img/xiaozhuang.jpg" v-show="flag" />
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
// 默认显示
flag:true
},
methods:{
// 显示/隐藏函数
showOrHide:function(){
// 将值修改为上次的取反
this.flag = !this.flag
}
}
})
script>
html>
与v-show效果一样,通过真假控制页面是否显示指定元素
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<button type="button" @click="isShow">点我切换状态button>
<p v-if="show">v-if控制是否显示p>
<p v-show="show">v-show控制是否显示p>
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
show:true
},
methods:{
isShow:function(){
this.show = !this.show
}
}
})
script>
html>
通过下方运行效果大家可以看出来,v-if隐藏是控制DOM也就是直接删除元素,而v-show是控制display
设置元素属性,比如src,title,type等
<div id="app">
<img src="图片地址">
<img v-bind:src="vue变量">
<img :src="vue变量">
div>
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<style type="text/css">
.active {
width: 200px;
height: 200px;
}
style>
head>
<body>
<div id="app">
<img v-bind:src="imgSrc" :class="isActive?'active':' '" @click="change"/>
div>
body>
<script>
var app = new Vue({
el:"#app",
data:{
imgSrc:"./img/xiaozhuang.jpg",
isActive:false
},
methods:{
change:function(){
this.isActive = !this.isActive
}
}
})
script>
html>
根据数据循环生成标签,并可以填充数据
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<h3>不带序号的前端路线h3>
<ul>
<li v-for="item in arr">{
{item}}li>
ul>
<h3>带序号的前端路线h3>
<ul>
<li v-for="(item,index) in arr">{
{index + 1}}、{
{item}}li>
ul>
<h3>对象类型数据h3>
<p v-for="item in food">{
{item.name}}p>
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
// 声明课程数组
arr:["HTML","CSS","JavaScript","jQuery"],
// 对象数据
food:[
{
"name":"河南烩面"},
{
"name":"陕西油泼面"}
]
}
})
script>
html>
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="list">
<button type="button" @click="add">添加课程button>
<button type="button" @click="del">删除课程button>
<p v-for="item in arr">{
{item.name}}p>
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#list",
data:{
arr:[
{
"name":"Coding课堂Vue课程"},
{
"name":"Coding课堂React课程"},
{
"name":"Coding课堂uni-app课程"}
]
},
methods:{
add:function(){
// 数组添加数据
this.arr.push({
"name":"添加的Vue课程"})
},
del:function(){
// 数组删除数据
this.arr.shift()
}
}
})
script>
html>
设置和获取表单元素的值(双向数据绑定)
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
head>
<body>
<div id="app">
<button @click="getMsg">获取数据button>
<br>
<input type="text" v-model="msg"/>
<p>{
{msg}}p>
div>
body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
msg:"Coding课堂"
},
methods:{
getMsg:function(){
alert(this.msg)
}
}
})
script>
html>
本节主要说了下Vue的一些常用指令,下一篇我们说几个Vue的实战案例,强化一下使用基础,欢迎持续关注。
本文若有任何看不懂,或者有错误的地方欢迎大家评论区留言,我时时关注哦