Vue3 引入axios报错 Cannot read properties of undefined (reading ‘post‘)

报错信息:

Uncaught TypeError: Cannot read properties of undefined (reading 'post')

报错截图:

login.vue 代码:

<template>
  <div id="login_header">欢迎登录</div>
  <div id="login_container">
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
        </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="input" placeholder="Please input password" show-password/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
  </el-form>
  </div>
</template>

<script>

  export default {
    // eslint-disable-next-line vue/multi-word-component-names
    name:'Login',
    data(){
      return {
        form:{
          username:'',
          password:''
        }
      }
    },
    methods:{
      onSubmit(){
        console.log('用户名:'+this.form.username)
        //使用axios把前端请求发送到后端
        this.$axios.post('http://localhost:8081/login',
            {'username':this.form.username,'password':this.form.password})
            .then(res => {
              console.log(res)
            })
      }
    }
  }
</script>
<style scoped>
#login_container{
  width: 40%;
  margin: 0 auto;
}
#login_header{
  text-align: center;
  margin-top: 50px;
  margin-bottom: 30px;
  font-size: 25px;
  color: #42b983;
}
</style>

 经检查发现,2个问题:

  1. 没有引入axios:
    import axios from 'axios'
  2. Vue3 引用代码不同于Vue2。改成
    axios.post('http://localhost:8081/login',
        {'username':this.form.username,'password':this.form.password})
        .then(res => {
          console.log(res)
        })

修改后,解决问题。

<template>
  <div id="login_header">欢迎登录</div>
  <div id="login_container">
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
        </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="input" placeholder="Please input password" show-password/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
  </el-form>
  </div>
</template>

<script>
import axios from 'axios'
  export default {
    // eslint-disable-next-line vue/multi-word-component-names
    name:'Login',
    data(){
      return {
        form:{
          username:'',
          password:''
        }
      }
    },
    methods:{
      onSubmit(){
        console.log('用户名:'+this.form.username)
        //使用axios把前端请求发送到后端
        axios.post('http://localhost:8081/login',
            {'username':this.form.username,'password':this.form.password})
            .then(res => {
              console.log(res)
            })
      }
    }
  }
</script>
<style scoped>
#login_container{
  width: 40%;
  margin: 0 auto;
}
#login_header{
  text-align: center;
  margin-top: 50px;
  margin-bottom: 30px;
  font-size: 25px;
  color: #42b983;
}
</style>

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值