Vue-Day 01 小案例练习

1.翻转数组

Vue-Day 01 案例分享 - 图1

  1. <template>
  2. <div>
  3. <h1>{{msg}}</h1>
  4. <button @click="reverse">翻转世界</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'App',
  10. data(){
  11. return{
  12. msg:'hello world',
  13. }
  14. },
  15. methods:{
  16. reverse(){
  17. const arr = this.msg.split("");
  18. arr.reverse();
  19. this.msg=arr.join("");
  20. }
  21. }
  22. }
  23. </script>
  24. <style>
  25. </style>

2.折叠面板

Vue-Day 01 案例分享 - 图2

  1. <template>
  2. <div>
  3. <h1>静夜思<button @click="control">{{show ? '收起' : '展开'}}</button></h1>
  4. <div v-if="show">
  5. <p>床前明月光</p>
  6. <p>疑是地上霜</p>
  7. <p>举头望明月</p>
  8. <p>低头思故乡</p>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: "App",
  15. data(){
  16. return{
  17. show:true
  18. }
  19. },
  20. methods:{
  21. control(){
  22. this.show = !this.show;
  23. }
  24. }
  25. };
  26. </script>
  27. <style>
  28. </style>

3.综合小案例

Vue-Day 01 案例分享 - 图3

  1. <template>
  2. <div>
  3. <!-- 标题部分 -->
  4. <h1 class="title" v-show="!show">
  5. {{title}}
  6. <span class="tag">{{cs}}</span>
  7. <a class="edit-btn" @click="xs">编辑</a>
  8. </h1>
  9. <!-- 编辑内容 -->
  10. <div v-show="show">
  11. <div class="input-group">标题:<input type="text" v-model="writeTitle"/></div>
  12. <div class="input-group">
  13. 频道:<select v-model="changeCs">
  14. <option value="前端">前端</option>
  15. <option value="测试">测试</option>
  16. <option value="Java">Java</option>
  17. </select>
  18. </div>
  19. <div class="operation">
  20. <button class="cancel" @click="show = false">取消</button>
  21. <button class="confirm" @click="ok">确认</button>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. // 1.先让输入框的内容隐藏
  29. // 2.当点击编辑的时候 显示输入框 并且 隐藏标题栏 让标题取反
  30. // 3.点击取消的 退回标题栏 <button class="cancel" @click="show = false">取消</button>
  31. // 点击确认 退回标题栏
  32. // 4.把标题和频道 先用数据绑定好 写死
  33. // 5.更新数据 让标题 和 频道的数据双向数据绑定 一改则改
  34. // 当点击编辑 打开编辑框时 原来的数据 会显示在输入框上
  35. data(){
  36. return{
  37. show:false,
  38. title:'hello world',
  39. cs:'前端',
  40. writeTitle:'',
  41. changeCs:'',
  42. }
  43. },
  44. methods:{
  45. // 点击编辑 显示到输入框上 <= 旧数据
  46. xs(){
  47. this.show = true;
  48. this.writeTitle = this.title;
  49. this.changeCs = this.cs;
  50. },
  51. // 点击确定 更新掉旧数据 new显示到标题上 <= 新数据
  52. ok(){
  53. // 当输入框为空 确定回不到标题上
  54. if(this.writeTitle===''){
  55. return;
  56. }
  57. this.show = false;
  58. this.title = this.writeTitle;
  59. this.cs = this.changeCs;
  60. }
  61. }
  62. };
  63. </script>
  64. <style>
  65. .title {
  66. display: flex;
  67. align-items: center;
  68. }
  69. .tag {
  70. margin-left: 8px;
  71. padding: 3px 6px;
  72. border-radius: 8px;
  73. background-color: green;
  74. font-size: 0.6em;
  75. color: #fff;
  76. }
  77. .edit-btn {
  78. margin-left: 32px;
  79. font-size: 0.8em;
  80. color: lightskyblue;
  81. }
  82. .input-group {
  83. margin-top: 16px;
  84. font-size: 28px;
  85. }
  86. .input-group input,
  87. .input-group select {
  88. width: 300px;
  89. border-color: #ddd;
  90. font-size: 28px;
  91. }
  92. .operation {
  93. margin-top: 16px;
  94. }
  95. .cancel {
  96. margin-left: 85px;
  97. background-color: #ddd;
  98. }
  99. .cancel,
  100. .confirm {
  101. padding: 8px 24px;
  102. border: 0;
  103. border-radius: 4px;
  104. }
  105. .confirm {
  106. margin-left: 16px;
  107. background-color: #006699;
  108. color: #fff;
  109. }
  110. </style>

空白案例 可以对齐进行练习

  1. <template>
  2. <div>
  3. <!-- 标题部分 -->
  4. <h1 class="title">
  5. 标题
  6. <span class="tag">测试</span>
  7. <a class="edit-btn">编辑</a>
  8. </h1>
  9. <!-- 编辑内容 -->
  10. <div>
  11. <div class="input-group">标题:<input type="text" /></div>
  12. <div class="input-group">
  13. 频道:<select>
  14. <option value="前端">前端</option>
  15. <option value="测试">测试</option>
  16. <option value="Java">Java</option>
  17. </select>
  18. </div>
  19. <div class="operation">
  20. <button class="cancel">取消</button>
  21. <button class="confirm">确认</button>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. };
  29. </script>
  30. <style>
  31. .title {
  32. display: flex;
  33. align-items: center;
  34. }
  35. .tag {
  36. margin-left: 8px;
  37. padding: 3px 6px;
  38. border-radius: 8px;
  39. background-color: green;
  40. font-size: 0.6em;
  41. color: #fff;
  42. }
  43. .edit-btn {
  44. margin-left: 32px;
  45. font-size: 0.8em;
  46. color: lightskyblue;
  47. }
  48. .input-group {
  49. margin-top: 16px;
  50. font-size: 28px;
  51. }
  52. .input-group input,
  53. .input-group select {
  54. width: 300px;
  55. border-color: #ddd;
  56. font-size: 28px;
  57. }
  58. .operation {
  59. margin-top: 16px;
  60. }
  61. .cancel {
  62. margin-left: 85px;
  63. background-color: #ddd;
  64. }
  65. .cancel,
  66. .confirm {
  67. padding: 8px 24px;
  68. border: 0;
  69. border-radius: 4px;
  70. }
  71. .confirm {
  72. margin-left: 16px;
  73. background-color: #006699;
  74. color: #fff;
  75. }
  76. </style>