Day21 vue.js网站删除特定文章

延续昨日
今天来完善我们的 我的专案的功能吧!
首先这是目前的我的专案
再来新增2个按钮
https://ithelp.ithome.com.tw/upload/images/20211003/20141007gIP16gEFe3.png
嘟嘟噜新增了2个按钮
https://ithelp.ithome.com.tw/upload/images/20211003/20141007oiHanuZccL.png
https://ithelp.ithome.com.tw/upload/images/20211003/20141007A4AViB1MoD.png
再来先从删除文章开始
先观察我们的db.json
https://ithelp.ithome.com.tw/upload/images/20211003/201410077PxFbQOdO8.png

进入postman
https://ithelp.ithome.com.tw/upload/images/20211003/20141007r4owBY3PJi.png
https://ithelp.ithome.com.tw/upload/images/20211003/20141007SkCD5521Cl.png
https://ithelp.ithome.com.tw/upload/images/20211003/201410074zG5yCjTlp.png

发现是怎麽接取并删除特定文章的资料之後
先在 methods创立function deleteArticles(val)

   methods:{
         async GetSession(){
          let user=sessionStorage.getItem('user-info');
       if(!user){
         alert("请先登入")
         this.$router.push({name:'Login'})
       }
      
      this.useraccount=JSON.parse(user).id
      this.username=JSON.parse(user).username
      let articles =await axios.get(`${this.db_api}`+"articles?useraccount="+`${this.useraccount}`)
      this.projects=articles.data

      },  

         async deleteArticles(val){
         let result =await axios.delete(`${this.db_api}`+"articles/"+val);
         console.warn(result)
     
       },

然後在 v-btn 里面设定 这个funcition并且接值(值的内容是project.id)
https://ithelp.ithome.com.tw/upload/images/20211003/201410070cuacaXNt9.png

再来就是打开console看看https://ithelp.ithome.com.tw/upload/images/20211003/20141007k1gVJMyNTq.png

Status是200 并且已经成功删除文章了
但是网页并没有马上更新 这时候就使用我们的 get session重新抓
忘记GetSession的参考day20

  methods:{
         async GetSession(){
          let user=sessionStorage.getItem('user-info');
       if(!user){
         alert("请先登入")
         this.$router.push({name:'Login'})
       }
      
      this.useraccount=JSON.parse(user).id
      this.username=JSON.parse(user).username
      let articles =await axios.get(`${this.db_api}`+"articles?useraccount="+`${this.useraccount}`)
      this.projects=articles.data

      },  

         async deleteArticles(val){
         let result =await axios.delete(`${this.db_api}`+"articles/"+val);
         console.warn(result)
        if(result.status==200){
          alert('已删除')
          this.GetSession();
        }
       },
    }

所以这就是现在的function
这样一来删除後就会直接渲染画面
p.s.为什麽有2个day17的文章 纯粹我懒 复制day17的资料後改了id就上
https://ithelp.ithome.com.tw/upload/images/20211003/201410078NmIhHe7uY.png

目前测试下来不会有不能删除的问题 所以也不用设定删除失败的告示
至於我没有设定一个确定删除吗的双重确认(我自己是觉得没必要啦等之後有空再来加)

最後帮我自己做一个小补充
Q:为什麽我按删除的时候 能正确删除我要删除的那笔文章
A:我用v-for把所有我的文章列印出来 我在删除文章的按妞中有接值 而值的内容是project.id 也就是 db.json里面的id这样一来即使两篇文章的title都一样
但是projcect.id也不一样 只是project.id并没有秀出来而已

明天就从修改文章开始吧
我们明天见!


<<:  Day 28 权限宝石:IAM Role 建立与使用

>>:  【Day 18】Shellcode 与他的快乐夥伴 (上) - Shellcode Loader

好想中乐透啊,Ruby 30 天刷题修行篇第十四话

嗨我是A Fei,连续好几天都十一点多回家,真的是累翻,先来看看今天的题目: Time to win...

【设计+切版30天实作】|Day12 - 怎麽让使用者选中您想要他选的Plans设计?

设计大纲 今天要来设计「方案」,外面的网站通常会有3个方案供使用者选择。另外如果要吸引使用者注册的话...

Day14:铁口直断

还记得当初应徵人生第一份软件工程师的Java笔试,那时候没有特别准备,但前面的一些类别物件概念都还能...

Day26 Lab 2 - Object storage 上传续传

https://github.com/kaichiachen/python-simple-objec...

[鼠年全马] W37 - Vue出一个旅馆预约平台(11)

这周接续上周进度 把真实资料喂进 SuccessCard.vue 罗~ #左侧预约功能(接续) 真实...