美文网首页
小练习两部分

小练习两部分

作者: 糖醋里脊120625 | 来源:发表于2023-11-22 14:40 被阅读0次
    function ceshi() {
      console.log("測試")
     
    
      const fn1 = () =>{
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(1);
        }, 300);
      });
    }
    const fn2 = () =>{
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(2);
        }, 600);
      });
    }
    const fn = () =>{
       Promise.all([fn1(),fn2()]).then(res =>{
           console.log(res);// [1,2]
       }) 
    }
    fn()
    
    
      return
      const deps = {
      '采购部':[1,2,3],
      '人事部':[5,8,12],
      '行政部':[5,14,79],
      '运输部':[3,64,105],
      }
      let member = [];
      for (let item in deps){
          const value = deps[item];
          if(Array.isArray(value)){
              member = [...member,...value]
          }
      }
      member = [...new Set(member)]
    
      console.log(member)
    
      return
      const erer = [1, 2, 3, 4, 5];
      const sddsd = erer.find(
        item => {
          return item === 3
        }
      )
      console.log(sddsd)
    
      return
      const addd = [1, 2, 3, 4, 5];
      const result = addd.filter(
        item => {
          return item > 3
        }
      )
      console.log(result)
    
      return
      // 数组操作
      const a = [1, 2, 3];
      const b = [1, 5, 6];
      const c = [...new Set([...a, ...b])];//[1,2,3,5,6]
      console.log(c)
    
      const obj1 = {
        a: 1,
      }
      const obj2 = {
        b: 1,
      }
      const obj = { ...obj1, ...obj2 };//{a:1,b:1}
      console.log(obj)
    }
    
    onMounted(() => {
      console.log(userInfoObj)
    });
    
    
    <script lang="ts" setup>
    import { computed, watch, onMounted } from 'vue'
    import LoadingTag from '@/components/LoadingTag/LoadingTag.jsx'
    import MusicCard from '@/components/MusicCard.vue'
    import RefreshCard from '@/components/RefreshCard.vue'
    import { HomeHooks } from './hooks/HomeHooks'
    import { refreshStore } from '@/stores/index'
    
    // 引入数据响应模块
    const { loading, noData, musicData, fetchMusicInfo } = HomeHooks()
    
    
    fetchMusicInfo()
    var userInfoObj = computed(() => refresher.getUserLevel);
    
    // 点击重试
    const retryData = () => {
      fetchMusicInfo()
    }
    // 点击
    function dianjiFun() {
      refresher.singleMusic()
      setTimeout(() => (console.log(refresher.umsicInfo)), 110)
    }
    
    
    
    
    
    
    // console.log(dd.#weight);//报错
    
    function ceshi() {
    
    
    
    
      //声明两个promise对象
      const p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('商品数据 - 1');
        }, 1000)
      });
    
      const p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve('商品数据 - 2');
          reject('出错啦!');
        }, 1000)
      });
    
      //调用 allsettled 方法,接受promise数组
      var result = Promise.allSettled([p1, p2]);
      //返回promise对象,且其结果状态永远成功。就算p2是reject
      console.log(result);
    
      const res = Promise.all([p1, p2]);
      //返回promise对象,且其结果状态由p1p2决定,都成功才成功 
      console.log(res);
    
    
      async function msain() {
        try {
          let DSD = await res;//await返回的是成功的PromiseResult值
          let dQAD = await result;//await返回的是成功的PromiseResult值
          console.log(dQAD);//用户数据
          console.log(DSD);//用户数据
        } catch (e) {
          console.log(e);//catch的是失败的PromiseResult值
        }
      }
      msain()
      return
    
      class Person {
        //公有属性
        name;
        //私有属性
        age;
        weight;
        //构造方法
        constructor(name: string, age: number, weight: string) {
          this.name = name;
          this.age = age;
          this.weight = weight;
        }
    
        intro() {
          console.log(this.name);
          console.log(this.age);//88
          console.log(this.weight);//100kg
          return "naiho"
        }
      }
    
      //实例化
      const dd = new Person('夏侯', 88, '100kg');
    
      // console.log(dd.name);
      console.log(dd.intro());
      // console.log(dd.#age);//报错
    
      return
      //数组和对象互换
      const school = {
        name: "葡葡",
        cities: ['皮皮', '秋秋', '小皮小葡'],
        xueke: ['前端', 'Java', '大数据', '运维']
      };
    
      //获取对象所有的键
      console.log(Object.keys(school));
      //获取对象所有的值
      console.log(Object.values(school));
      //entries:将一个对象转化为一个二维数组
      console.log(Object.entries(school));
    
      return
      var computers = [
        { name: "Apple", ram: 8 },
        { name: "IBM", ram: 4 },
        { name: "Acer", ram: 32 },
      ];
      var result = computers.every(function (computer) {
        return computer.ram > 3
      })
      console.log(result)//true;
    
    
      var somData = computers.some(function (computer) {
        return computer.ram > 30
      })
      console.log(somData)//true;
    
      var res3 = computers.find(item => {
        return item.name === "Acer"
      })
      console.log(res3);
    
    
      let arr = [3, 4, 5]
      console.log(...arr) // 3 4 5
    
      //遍历:for-in
      let obj = { name: 'Domesy', value: 'React' }
    
      for (let key in obj) {
        console.log(key); // 依次返回属性值 name value
        console.log(obj[key]); // 依次返回属性值 Domesy React
      }
    
    
    
    
    
    
    
      return
      main();
    
    
    
    
    
    }
    
    
    const p = new Promise((resolve, reject) => {
      resolve("用户数据");
      // reject("失败啦!");
    })
    
    // await 要放在 async 函数中.
    async function main() {
      try {
        let result = await p;//await返回的是成功的PromiseResult值
        console.log(result);//用户数据
      } catch (e) {
        console.log(e);//catch的是失败的PromiseResult值
      }
    }
    //调用函数
    
    
    
    onMounted(() => {
      console.log(userInfoObj)
    });
    
    
    const refresher = refreshStore()
    
    
    
    watch(
      computed(() => {
        // 接收store,返回ref对象,并监听
        return refresher.refreshNum
      }),
      (newVal, oldVal) => {
        console.log(newVal)
        console.log('执行')
        console.log(oldVal)
        // 监听值变化,刷新数据
        if (newVal === oldVal) return
        retryData()
      }
    )
    
    </script>
    

    相关文章

      网友评论

          本文标题:小练习两部分

          本文链接:https://www.haomeiwen.com/subject/lbtwwdtx.html