v-bind
<template>
<div>
<a v-bind:href="url">跳转到百度</a>
<a :href="url">跳转到百度</a>
</div>
</template>
<script>
export default {
data() {
return {
url: "http://baidu.com",
};
},
};
</script>
<style scoped>
</style>
v-on
<template>
<div>
<p>要买的商品数:{{ count }}</p>
<button v-on:click="count = count + 1">增加1</button>
<button v-on:click="addFun">增加1</button>
<button v-on:click="addFunc(5)">增加</button>
</div>
</template>
<script>
export default {
data() {
return {
count: 0,
};
},
methods: {
addFun() {
this.count++;
},
addFunc(num) {
this.count += num;
},
},
};
</script>
<style scoped>
</style>
@事件名.修饰符="methods里函数
<template>
<div @click="faFn">
<button @click.stop="btn">组织事件冒泡</button>
<a href="http://baidu.com" @click.prevent="btn">阻止默认行为</a>
<a href="http://baidu.com" @click.once="btn2">只执行一次</a>
</div>
</template>
<script>
export default {
methods: {
faFn() {
console.log("1");
},
btn() {
console.log("2");
},
btn2() {
console.log("3");
},
},
};
</script>
<style scoped>
</style>
v-on按键修饰符
<template>
<div>
<input type="text" @keydown.enter="enterFn" />
<input type="text" @keyup.esc="enterFnc" />
</div>
</template>
<script>
export default {
methods: {
enterFn() {
console.log("1");
},
enterFnc() {
console.log("2");
},
},
};
</script>
翻转案例(message报错)
<template>
<div>
<h1>{{ message }}</h1>
<button @click="btn">翻转</button>
</div>
</template>
<script>
export default {
data() {
return {
message: "hello,world",
};
},
methods: {
btn() {
const arr = this.message.split("");
console.log(arr);
const arrRe = arr.reverse();
console.log(arrRe);
message = arrRe.join(" ");
console.log(message);
},
},
};
</script>
<style scoped>
</style>
v-model(看视频)
<template>
<div @click="touch">
<div>
<span>来自:</span>
<select v-model="form">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
</select>
</div>
<div>
<span>爱好:</span>
<input type="checkbox" v-model="hobby" value="抽烟" />抽烟
<input type="checkbox" v-model="hobby" value="喝酒" />喝酒
<input type="checkbox" v-model="hobby" value="烫头" />烫头
</div>
<div>
<span>年龄:</span>
<input type="text" v-model.number="age" />
</div>
<div>
<span>人生格言:</span>
<input type="text" v-model.trim="motto" />
</div>
<div>
<span>自我介绍:</span>
<textarea v-model.lazy="intro"></textarea>
</div>
</div>
</template>
<script>
export default {
data() {
return {
form: "1",
hobby: [],
age: "",
motto: "",
intro: "",
};
},
methods: {
touch() {
console.log("1");
},
},
};
</script>
<style scoped>
</style>
v-text和v-html
<template>
<!--v-text和v-html
v-text="vue数据变量"
v-html="vue数据变量"
会覆盖插值表达式
-->
<div>
<p v-text="str"></p>
<p v-html="str"></p>
</div>
</template>
<script>
export default {
data() {
return {
str: "<span>我是一个标签</span> ",
};
},
};
</script>
<style>
</style>
v-show和v-if(看视频)
<template>
<!--v-show和v-if
v-show="vue变量"
v-show 用的display:none隐藏 (频繁切换使用)
-->
<div>
<h1 v-show="str">v-show</h1>
<h1 v-if="str">v-if</h1>
<div>
<p v-if="age > 18">v-if</p>
<p v-else></p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
str: true,
age: 15,
};
},
};
</script>
<style>
</style>
折叠面板案例
<template>
<div id="app">
<h3>案例:折叠面板</h3>
<div>
<div class="title">
<h4>芙蓉楼送辛渐</h4>
<span class="btn" @click="isshow = !isshow">
{{ isshow ? "收起" : "展开" }}
</span>
</div>
<div class="container" v-show="isshow">
<p>寒雨连江夜入吴,</p>
<p>平明送客楚山孤。</p>
<p>洛阳亲友如相问,</p>
<p>一片冰心在玉壶。</p>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
isshow: false,
};
},
};
</script>
<style lang="less">
body {
background-color: #ccc;
#app {
width: 400px;
margin: 20px auto;
background-color: #fff;
border: 4px solid blueviolet;
border-radius: 1em;
box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
padding: 1em 2em 2em;
h3 {
text-align: center;
}
.title {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid #ccc;
padding: 0 1em;
}
.title h4 {
line-height: 2;
margin: 0;
}
.container {
border: 1px solid #ccc;
padding: 0 1em;
}
.btn {
cursor: pointer;
}
}
}
</style>
v-for
<template>
<div>
<div>
<p>学生姓名</p>
<ul>
<li v-for="(item, index) in arr" :key="item">{{ index }}-{{ item }}</li>
</ul>
<p>学生详细信息</p>
<ul>
<li v-for="obj in stuArr" :key="obj.id">
<span>{{ obj.name }} </span>
<span>{{ obj.sex }} </span>
<span>{{ obj.hobby }} </span>
</li>
</ul>
</div>
<p>序号</p>
<div v-for="i in count" :key="i">{{ i }}</div>
</div>
</template>
<script>
export default {
data() {
return {
arr: ["小红", "小兰", "小绿"],
stuArr: [
{
id: 1001,
name: "孙悟空",
sex: "男",
hobby: "吃桃子",
},
{
id: 1002,
name: "猪八戒",
sex: "男",
hobby: "背媳妇",
},
],
count: 10,
};
},
};
</script>
<style>
</style>