当前位置:   article > 正文

vue+uni-app案例

vue+uni-app案例

 1.vue案例(包含删除,新增,和回车,点击事件)

  1. <template>
  2. <div>
  3. <h1>Todo List</h1>
  4. <input v-model="newItem" placeholder="Add new item" @keyup.enter="addNewItem()" />
  5. <button @click="addNewItem">Add Item</button>
  6. <ul>
  7. <li v-for="(item, index) in items" :key="index">
  8. {{ item }}
  9. <button @click="removeItem(index)">Remove</button>
  10. </li>
  11. </ul>
  12. </div>
  13. </template>
  14. <script setup>
  15. import { ref } from 'vue';
  16. const items = ref([
  17. 'Learn Vue',
  18. 'Build something awesome',
  19. 'Profit!',
  20. ]);
  21. const newItem = ref('');
  22. function addNewItem() {
  23. items.value.push(newItem.value);
  24. newItem.value = '';
  25. }
  26. function removeItem(index) {
  27. items.value.splice(index, 1);
  28. }
  29. </script>

2.uni-app案例(包含删除,新增,和回车,点击事件)

  1. <template>
  2. <view class="out">
  3. <view class="top">
  4. <h1>近期热搜</h1>
  5. </view>
  6. <view class="body">
  7. <view class="forBody" v-for="(item,index) in titles" :key="item.id">
  8. <view class="textBody">
  9. <span class="title">{{index+1}}.</span>
  10. <span class="text">{{item.name}}</span>
  11. <span class="del" @click="del(index)">删除</span>
  12. </view>
  13. </view>
  14. <view class="num">共{{titles.length}}条热搜</view>
  15. </view>
  16. <view class="buttom">
  17. <input type="text" auto-focus="true" v-model="text" class="tex" placeholder="请输入热搜" @confirm="insert()">
  18. <button class="bton" @click="insert"><span class="btonText">添加</span></button>
  19. <button @click="remover()"><span>清空</span></button>
  20. </view>
  21. </view>
  22. </template>
  23. <script setup>
  24. import {
  25. ref
  26. } from 'vue';
  27. var remover = () => {
  28. titles.value=[]
  29. }
  30. var titles = ref([{
  31. id: 1,
  32. name: '老王被抓了??'
  33. },
  34. {
  35. id: 2,
  36. name: '日本被灭了'
  37. },
  38. {
  39. id: 3,
  40. name: '山中无老虎,台湾称王??'
  41. },
  42. {
  43. id: 4,
  44. name: '台湾回归?'
  45. },
  46. {
  47. id: 5,
  48. name: '重生之新一是首富??'
  49. },
  50. {
  51. id: 6,
  52. name: '早恋被逮到了??'
  53. }
  54. ])
  55. var del = (index) => {
  56. console.log(index);
  57. titles.value.splice(index, 1)
  58. }
  59. var i = titles.value.length + 1;
  60. var insert = () => {
  61. console.log(text);
  62. titles.value.push({
  63. name: text.value,
  64. id: i
  65. })
  66. i++;
  67. text.value = ''
  68. }
  69. var text = ref('');
  70. </script>
  71. <style>
  72. .top {
  73. text-align: center;
  74. font-size: 30px;
  75. margin-bottom: 20px;
  76. }
  77. .buttom {
  78. margin-top: 5px;
  79. text-align: center;
  80. }
  81. .textBody {
  82. margin: auto;
  83. width: 80%;
  84. height: 30px;
  85. border-bottom: 1px solid red;
  86. position: relative;
  87. }
  88. .del {
  89. position: absolute;
  90. right: 20px;
  91. color: blue;
  92. }
  93. .num {
  94. margin-top: 5px;
  95. text-align: center;
  96. }
  97. .tex {
  98. display: inline-block;
  99. width: 60%;
  100. height: 30px;
  101. border: 1px solid gray;
  102. }
  103. .bton {
  104. display: inline-block;
  105. width: 30%;
  106. height: 30px;
  107. line-height: 30px;
  108. color: aliceblue;
  109. background-color: red;
  110. }
  111. </style>

3. uni-ap计算案例(computed 案例,以及checkbox-group使用方法)

  1. <template>
  2. <view class="body">
  3. <checkbox-group @change="itemChange">
  4. <!-- 遍历数据项,生成可选择的项 -->
  5. <view class="forBody" v-for="(item) in data" :key="item.id">
  6. <view class="item">
  7. <!-- 显示数据项的id、名称和价格 -->
  8. <span class="span">{{item.id}}</span>
  9. <span class="span">{{item.name}}</span>
  10. <span class="span">{{item.price}}</span>
  11. <!-- 包含选择框,其值为数据项的价格 -->
  12. <span class="span">
  13. <checkbox :value="item.price"></checkbox>
  14. </span>
  15. </view>
  16. </view>
  17. </checkbox-group>
  18. <!-- 显示总价 -->
  19. {{sumPrice}}
  20. </view>
  21. </template>
  22. <script setup>
  23. import {
  24. computed,
  25. ref
  26. } from 'vue';
  27. // 定义数据数组,包含id、名称和价格,初始全选状态
  28. var data = ref([{
  29. id: 1,
  30. name: '苹果',
  31. price: '100',
  32. check: true
  33. },
  34. {
  35. id: 2,
  36. name: '华为',
  37. price: '200',
  38. check: true
  39. },
  40. {
  41. id: 3,
  42. name: 'poop',
  43. price: '300',
  44. check: true
  45. }, {
  46. id: 4,
  47. name: 'vivo',
  48. price: '400',
  49. check: true
  50. },
  51. ]);
  52. // 用于存储用户选择的项的价格
  53. var checkItem = ref([]);
  54. // 计算总价,基于用户选择的项
  55. var sumPrice = computed(() => {
  56. let sum = 0;
  57. // 遍历数据项,检查是否被选中,如果选中则累加价格
  58. data.value.forEach(item => {
  59. if (checkItem.value.indexOf(item.price) > -1) {
  60. sum += parseInt(item.price)
  61. }
  62. })
  63. return sum;
  64. });
  65. // 处理选择事件,更新用户选择的数据
  66. var itemChange = (e) => {
  67. checkItem.value = e.detail.value
  68. }
  69. </script>
  70. <style scoped lang="scss">
  71. // 设置每个数据项显示内容之间的间距
  72. .span {
  73. margin-right: 20px;
  74. }
  75. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1021710
推荐阅读
相关标签
  

闽ICP备14008679号