【问题标题】:Trying to send the file name as a prop to a child component using Vue 3尝试使用 Vue 3 将文件名作为道具发送到子组件
【发布时间】:2023-02-06 17:02:16
【问题描述】:

我不确定我的代码有什么问题。我希望更新常量“声音”,但我尝试了很多方法,“声音”属性的值仍然不会根据数据库发生变化

  const computed = {
    sounds: {
      get() {
        return this.mcollection1Data.value;
      },
      set(value) {
        this.mcollection1Data.value = value;
      }
    }
  };

下面是完整代码

<script setup>
  import { ref, onMounted } from 'vue';
  import axios from 'axios';
  import playsound from './playsound.vue'
   
  const sounds = ref([]); 

  const collection1Data = ref([]);
  const collection2Data = ref([]);
  const mcollection1Data = ref([]);
  const mcollection2Data = ref([]);

  const fetchData = async () => {
    const [collection1Response, collection2Response] = await Promise.all([
      axios.get('https://koh-xyn.com:50100/onboardshows'),
      axios.get('https://koh-xyn.com:50100/onboardlands'),
    ]);
    collection1Data.value = collection1Response.data;
    collection2Data.value = collection2Response.data;

    const currentTime = new Date().getTime();
    const filteredData1 = collection1Response.data.filter(doc => {
      const updatedTime = new Date(doc.updatedAt).getTime();
      return currentTime - updatedTime < 20 * 1000;
    });
    const filteredData2 = collection2Response.data.filter(doc => {
      const updatedTime = new Date(doc.updatedAt).getTime();
      return currentTime - updatedTime < 20 * 1000;
    });

    mcollection1Data.value = filteredData1;
    mcollection2Data.value = filteredData2;
  };

  const computed = {
    sounds: {
      get() {
        return this.mcollection1Data.value;
      },
      set(value) {
        this.mcollection1Data.value = value;
      }
    }
  };

  onMounted(() => {
    fetchData();
    setInterval(fetchData, 1000);
  });
  
        
  const isRecentlyUpdated = (updatedAt) => {
    const updatedTime = new Date(updatedAt);
    return (Date.now() - updatedTime.getTime()) < 15000;
  };

</script>

【问题讨论】:

  • set(value) { mcollection1Data.value = value; } 试试这行代码

标签: javascript vuejs3


【解决方案1】:

为什么您将“声音”定义为反应式和计算式?!?!

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2021-07-29
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多