uni-app 多文件搜索 点击搜索结果会自动覆盖原文件内容

uni-app 多文件搜索 点击搜索结果会自动覆盖原文件内容

开发环境 版本号 项目创建方式
HbuilderX 3.4.0

操作步骤:

  • 多文件搜索后,点击搜索结果列表

预期结果:

  • 右侧显示点击的文件内容

实际结果:

  • 右侧的文件内容,被自动修改

bug描述:

当我点击搜索结果列表时,莫名其妙的就将我的文件内容自动替换,我没有任何备份,导致内容丢失,很严重!!!!!!!!!!!!

image

日志.txt

2 回复

升级一下HBuilderX版本,目前是3.4.4-alpha版本,3.4.2已修复此问题


在 Uni-app 中,如果你正在开发一个多文件搜索功能,并且希望在点击搜索结果时自动覆盖原文件的内容,你可以按照以下步骤实现:

1. 实现搜索功能

首先,你需要实现一个搜索功能,能够在多个文件中查找匹配的内容。可以使用 uni.request 或者 uniCloud 来获取文件内容,并在前端进行搜索。

// 假设你有一个文件列表
const files = [
  { id: 1, name: 'file1.txt', content: 'This is the content of file 1.' },
  { id: 2, name: 'file2.txt', content: 'This is the content of file 2.' },
  // 更多文件...
];

// 搜索函数
function searchFiles(keyword) {
  const results = [];
  files.forEach(file => {
    if (file.content.includes(keyword)) {
      results.push({
        fileId: file.id,
        fileName: file.name,
        content: file.content
      });
    }
  });
  return results;
}

2. 显示搜索结果

将搜索结果展示在页面上,并为每个结果项添加点击事件。

<template>
  <view>
    <input v-model="searchKeyword" placeholder="请输入搜索关键词" />
    <button @click="handleSearch">搜索</button>
    <view v-for="(result, index) in searchResults" :key="index" @click="handleResultClick(result)">
      <text>{{ result.fileName }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      searchResults: []
    };
  },
  methods: {
    handleSearch() {
      this.searchResults = searchFiles(this.searchKeyword);
    },
    handleResultClick(result) {
      // 在这里处理点击事件,覆盖原文件内容
      this.overwriteFileContent(result.fileId, result.content);
    },
    overwriteFileContent(fileId, newContent) {
      // 根据 fileId 找到原文件并覆盖内容
      const file = files.find(f => f.id === fileId);
      if (file) {
        file.content = newContent;
        // 这里可以触发一个更新操作,比如保存到服务器或更新本地存储
      }
    }
  }
};
</script>

3. 更新文件内容

handleResultClick 方法中,你可以通过 overwriteFileContent 方法来更新文件内容。你可以根据需要将更新后的内容保存到服务器或本地存储中。

4. 处理文件更新后的逻辑

如果你需要实时更新页面上的内容,可以使用 Vue 的响应式机制来触发视图更新。例如,你可以在 overwriteFileContent 方法中更新 files 数组,这样页面上显示的内容会自动更新。

5. 保存更改(可选)

如果你需要将更改保存到服务器或本地存储中,可以在 overwriteFileContent 方法中添加相应的逻辑。

overwriteFileContent(fileId, newContent) {
  const file = files.find(f => f.id === fileId);
  if (file) {
    file.content = newContent;
    // 保存到服务器或本地存储
    this.saveFile(file);
  }
},
saveFile(file) {
  // 使用 uni.request 或 uniCloud 保存文件
  uni.request({
    url: 'https://your-api-endpoint.com/saveFile',
    method: 'POST',
    data: file,
    success: (res) => {
      console.log('文件保存成功', res.data);
    },
    fail: (err) => {
      console.error('文件保存失败', err);
    }
  });
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!