无法读取未定义的属性“ $ notify”

时间:2019-04-04 14:45:11

标签: vue.js element-ui

我正在尝试显示来自组件的通知。我的组件代码:

<script>
    export default {
        data () {
            return {

            }
        },

        methods:
        {
            save_data()
            {
                axios.post(base_url + '/api/settings/save', this.form)
                  .then(function (response) {
                   this.$notify({title: 'Saved', message: 'Settings Saved', position: 'bottom-right', type: 'success' });
                  })
                  .catch(function (error) {
                    console.log(error);

                  });
            }
        },

    }
</script>

但是我遇到错误:Cannot read property '$notify' of undefined。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您引用的对象错误。在axios回调中使用this将引用axios帖子。试试这个:

save_data() {
   let self = this;
   axios.post(base_url + '/api/settings/save', this.form)
      .then(function (response) {
         self.$notify({title: 'Saved', message: 'Settings Saved', position: 
         'bottom-right', type: 'success' });
      })
      .catch(function (error) {
         console.log(error);
      });
}