uniapp学习4,简易记事本2.0

note.vue

html 复制代码
<template>
  <view class="wrap">
    <view class="header">
      <text class="title">简易记事本</text>
    </view>

    <view class="search-box">
      <input v-model="keyword" placeholder="搜索笔记..." @input="doSearch" />
    </view>

    <view class="list">
      <view class="item" v-for="(item, idx) in showList" :key="idx">
        <!-- 点击进入编辑 -->
        <view class="content" @click="goEdit(idx)">
          <text class="txt">{{ item.title.length > 16 ? item.title.slice(0,16) + '...' : item.title }}</text>
          <text class="time">{{ item.time }}</text>
        </view>
        <!-- 删除按钮 -->
        <view class="del-btn" @click="delNote(idx)">删除</view>
      </view>

      <view class="empty" v-if="showList.length === 0">
        暂无笔记,点击右下角新增
      </view>
    </view>

    <view class="add-float-btn" @click="goAdd">
      <text class="add-icon">+</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      noteList: [],
      showList: [],
      keyword: ""
    };
  },
  onShow() {
    this.getList();
  },
  methods: {
    getList() {
      let list = uni.getStorageSync("noteList") || [];
      this.noteList = list;
      this.showList = list;
    },
    doSearch() {
      let k = this.keyword.trim();
      if (!k) {
        this.showList = this.noteList;
        return;
      }
      this.showList = this.noteList.filter(item => item.title.includes(k));
    },
    goAdd() {
      uni.navigateTo({
        url: "/pages/note/edit"
      });
    },
    goEdit(idx) {
      uni.navigateTo({
        url: `/pages/note/edit?idx=${idx}`
      });
    },
    // 删除笔记
    delNote(idx) {
      uni.showModal({
        title: "确认删除",
        content: "确定要删除这条笔记吗?",
        success: (res) => {
          if (res.confirm) {
            let list = JSON.parse(JSON.stringify(this.noteList));
            list.splice(idx, 1);
            uni.setStorageSync("noteList", list);
            this.getList();
            uni.showToast({ title: "删除成功" });
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.wrap {
  padding: 20rpx;
  background: #f7f8fa;
  min-height: 100vh;
}
.header {
  margin-bottom: 20rpx;
}
.title {
  font-size: 38rpx;
  font-weight: bold;
}
.search-box {
  background: #fff;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
}
.search-box input {
  font-size: 28rpx;
}
.list {
  background: #fff;
  border-radius: 12rpx;
  overflow: hidden;
}
.item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}
.content {
  flex: 1;
}
.txt {
  font-size: 30rpx;
  display: block;
  margin-bottom: 10rpx;
}
.time {
  font-size: 24rpx;
  color: #999;
}
/* 删除按钮 */
.del-btn {
  color: #ff0000;
  font-size: 26rpx;
  padding: 12rpx 20rpx;
  border-radius: 8rpx;
}
.empty {
  padding: 60rpx;
  text-align: center;
  color: #999;
}
.add-float-btn {
  position: fixed;
  right: 40rpx;
  bottom: 40rpx;
  width: 100rpx;
  height: 100rpx;
  border-radius: 50%;
  background: #42b983;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 16rpx rgba(66, 185, 131, 0.3);
  z-index: 999;
}
.add-icon {
  font-size: 50rpx;
  color: #fff;
  font-weight: bold;
}
</style>

edit.vue

html 复制代码
<template>
  <view class="wrap">
    <textarea
      v-model="content"
      class="input-area"
      placeholder="请输入笔记内容..."
      placeholder-class="gray"
    />
  </view>
</template>

<script>
export default {
  data() {
    return {
      content: "",
      idx: -1
    };
  },
  onLoad(options) {
    this.idx = options.idx || -1;
    if (this.idx > -1) {
      let list = uni.getStorageSync("noteList") || [];
      this.content = list[this.idx]?.title || "";
    }
  },
  onUnload() {
    this.save();
  },
  methods: {
    save() {
      let val = this.content.trim();
      if (!val) return;

      let list = uni.getStorageSync("noteList") || [];
      let time = new Date().toLocaleString();

      if (this.idx > -1) {
        list[this.idx] = { title: val, time };
      } else {
        list.unshift({ title: val, time });
      }

      uni.setStorageSync("noteList", list);
    }
  }
};
</script>

<style scoped>
.wrap {
  padding: 30rpx;
  background: #fff;
  min-height: 100vh;
}
.input-area {
  width: 100%;
  min-height: 80vh;
  font-size: 32rpx;
  line-height: 1.6;
}
</style>
相关推荐
电科_银尘13 小时前
【深度学习项目实战·22】灰度图像着色:用小型U-Net学习从灰度到RGB
人工智能·深度学习·学习
QuZhengRong14 小时前
【AI】Agent 全栈进阶|AgentScope 沙箱源码解析
python·学习·ai·agent·沙箱
承渊政道19 小时前
【Python编程—从入门到实践】(用户账户:给Django网站装上登录系统,让每个人拥有自己的学习笔记)
python·学习·pycharm·django
诗句藏于尽头20 小时前
游戏《完美神话》-使用Ai-agent进行修复已停服游戏到本地成功部署以及GM端网页完整教程
人工智能·学习·游戏
希和202720 小时前
51 单片机学习笔记:LCD1602、独立按键、I2C 与 AT24C02 掉电存储(江科大课程小白笔记)
笔记·单片机·学习
承渊政道21 小时前
【Python编程—从入门到实践】(设置样式并部署:给Django网站换新装,把学习笔记发布到互联网)
python·学习·pycharm·django
~kiss~1 天前
A Survey of On-Policy Distillation for LLMs 学习(一)
学习
小杨小杨、努力变强!1 天前
python学习笔记
笔记·学习
Star(up up up)2 天前
Redis Windows 安装教程:使用Windows 的 Redis 压缩包
redis·笔记·学习·程序人生·云原生·学习方法·改行学it
乌暮2 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring