代码之家  ›  专栏  ›  技术社区  ›  user8359832

试图删除与Vuejs内部Axios的联系

  •  1
  • user8359832  · 技术社区  · 7 年前

    我正在开发一个应用程序来存储联系信息,并利用Vuejs和Laravel来做到这一点。我还使用axios库实现CRUD功能。

    我有这个错误 axios.delete() 我搞不懂。这是我的联系人。Vue文件:

    <script>
      export default {
        data: function(){
          return {
            edit:false,
            list:[],
            contact:{
              id:'',
              name:'',
              email:'',
              phone:''
            }
          }
        },
        mounted: function(){
          console.log('Contacts Component Loaded...');
          this.fetchContactList();
        },
        methods: {
          fetchContactList: function(){
            console.log('Fetching contacts...');
            axios.get('api/contacts').then((response) => {
              console.log(response.data);
              this.list = response.data;
            }).catch((error) => {
              console.log(error);
            });
          },
          createContact: function(){
            console.log('Creating contact...');
            let self = this;
            // merging params to the current object
            let params = Object.assign({}, self.contact);
            // pass above to axios request
            axios.post('api/contact/store', params)
              .then(function(){
                self.contact.name = '';
                self.contact.email = '';
                self.contact.phone = '';
                self.edit = false;
                self.fetchContactList();
              })
              .catch(function(error){
                console.log(error);
              });
          },
          showContact: function(id){
            let self = this;
            axios.get('api/contact/' + id)
              .then(function(response){
                self.contact.id = response.data.id;
                self.contact.name = response.data.name;
                self.contact.email = response.data.email;
                self.contact.phone = response.data.phone;
              })
              self.edit = true;
          },
          updateContact: function(id){
            console.log('Updating contact '+id+'...');
            let self = this;
            // merging params to the current object
            let params = Object.assign({}, self.contact);
            // pass above to axios request
            axios.patch('api/contact/'+id, params)
              .then(function(){
                self.contact.name = '';
                self.contact.email = '';
                self.contact.phone = '';
                self.edit = false;
                self.fetchContactList();
              })
              .catch(function(error){
                console.log(error);
              });
          },
          deleteContact: function(id){
            axios.delete('api/contact/'+id)
              .then(function(response){
                self.fetchContactList();
              })
              .catch(function(error){
                console.log(error);
              });
          }
        }
      }
    </script>
    

    self.fetchContactList is not a function .

    我知道它说值实际上不是一个函数。函数名中没有输入错误。我是否在错误的对象上调用了该函数?我应该使用不同的属性名称吗?

    我用过 self.fetchContactList(); 在添加和更新联系人时,为什么不删除联系人?

    我需要添加请求头吗?我不必为其他请求而担心。

    如果我只是删除 self.fetchContactList() 它根本不起作用。

    尽管出现了错误,但当我刷新页面时,它会删除联系人,但我希望在单击删除按钮时删除联系人。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Mirza Brunjadze    7 年前

    你没有 let self = this; 线路输入 deleteContact 函数,显然会出现错误。

    或者,您可以使用ES6箭头函数来避免分配 this 要像这样分离变量:

    deleteContact: function(id) {
      axios.delete('api/contact/'+id)
        .then((response) => {
          this.fetchContactList();
        })
        .catch((error) => {
          console.log(error);
        });
    }