Vue-Day 01 小案例练习
1.翻转数组
<template>
<div>
<h1>{{msg}}</h1>
<button @click="reverse">翻转世界</button>
</div>
</template>
<script>
export default {
name: 'App',
data(){
return{
msg:'hello world',
}
},
methods:{
reverse(){
const arr = this.msg.split("");
arr.reverse();
this.msg=arr.join("");
}
}
}
</script>
<style>
</style>
2.折叠面板
<template>
<div>
<h1>静夜思<button @click="control">{{show ? '收起' : '展开'}}</button></h1>
<div v-if="show">
<p>床前明月光</p>
<p>疑是地上霜</p>
<p>举头望明月</p>
<p>低头思故乡</p>
</div>
</div>
</template>
<script>
export default {
name: "App",
data(){
return{
show:true
}
},
methods:{
control(){
this.show = !this.show;
}
}
};
</script>
<style>
</style>
3.综合小案例
<template>
<div>
<!-- 标题部分 -->
<h1 class="title" v-show="!show">
{{title}}
<span class="tag">{{cs}}</span>
<a class="edit-btn" @click="xs">编辑</a>
</h1>
<!-- 编辑内容 -->
<div v-show="show">
<div class="input-group">标题:<input type="text" v-model="writeTitle"/></div>
<div class="input-group">
频道:<select v-model="changeCs">
<option value="前端">前端</option>
<option value="测试">测试</option>
<option value="Java">Java</option>
</select>
</div>
<div class="operation">
<button class="cancel" @click="show = false">取消</button>
<button class="confirm" @click="ok">确认</button>
</div>
</div>
</div>
</template>
<script>
export default {
// 1.先让输入框的内容隐藏
// 2.当点击编辑的时候 显示输入框 并且 隐藏标题栏 让标题取反
// 3.点击取消的 退回标题栏 <button class="cancel" @click="show = false">取消</button>
// 点击确认 退回标题栏
// 4.把标题和频道 先用数据绑定好 写死
// 5.更新数据 让标题 和 频道的数据双向数据绑定 一改则改
// 当点击编辑 打开编辑框时 原来的数据 会显示在输入框上
data(){
return{
show:false,
title:'hello world',
cs:'前端',
writeTitle:'',
changeCs:'',
}
},
methods:{
// 点击编辑 显示到输入框上 <= 旧数据
xs(){
this.show = true;
this.writeTitle = this.title;
this.changeCs = this.cs;
},
// 点击确定 更新掉旧数据 new显示到标题上 <= 新数据
ok(){
// 当输入框为空 确定回不到标题上
if(this.writeTitle===''){
return;
}
this.show = false;
this.title = this.writeTitle;
this.cs = this.changeCs;
}
}
};
</script>
<style>
.title {
display: flex;
align-items: center;
}
.tag {
margin-left: 8px;
padding: 3px 6px;
border-radius: 8px;
background-color: green;
font-size: 0.6em;
color: #fff;
}
.edit-btn {
margin-left: 32px;
font-size: 0.8em;
color: lightskyblue;
}
.input-group {
margin-top: 16px;
font-size: 28px;
}
.input-group input,
.input-group select {
width: 300px;
border-color: #ddd;
font-size: 28px;
}
.operation {
margin-top: 16px;
}
.cancel {
margin-left: 85px;
background-color: #ddd;
}
.cancel,
.confirm {
padding: 8px 24px;
border: 0;
border-radius: 4px;
}
.confirm {
margin-left: 16px;
background-color: #006699;
color: #fff;
}
</style>
空白案例 可以对齐进行练习
<template>
<div>
<!-- 标题部分 -->
<h1 class="title">
标题
<span class="tag">测试</span>
<a class="edit-btn">编辑</a>
</h1>
<!-- 编辑内容 -->
<div>
<div class="input-group">标题:<input type="text" /></div>
<div class="input-group">
频道:<select>
<option value="前端">前端</option>
<option value="测试">测试</option>
<option value="Java">Java</option>
</select>
</div>
<div class="operation">
<button class="cancel">取消</button>
<button class="confirm">确认</button>
</div>
</div>
</div>
</template>
<script>
export default {
};
</script>
<style>
.title {
display: flex;
align-items: center;
}
.tag {
margin-left: 8px;
padding: 3px 6px;
border-radius: 8px;
background-color: green;
font-size: 0.6em;
color: #fff;
}
.edit-btn {
margin-left: 32px;
font-size: 0.8em;
color: lightskyblue;
}
.input-group {
margin-top: 16px;
font-size: 28px;
}
.input-group input,
.input-group select {
width: 300px;
border-color: #ddd;
font-size: 28px;
}
.operation {
margin-top: 16px;
}
.cancel {
margin-left: 85px;
background-color: #ddd;
}
.cancel,
.confirm {
padding: 8px 24px;
border: 0;
border-radius: 4px;
}
.confirm {
margin-left: 16px;
background-color: #006699;
color: #fff;
}
</style>