满口金牙 2021-10-17 16:17 采纳率: 91.5%
浏览 66
已结题

Vue3.0 POST 传参错误, 请指点!


<template>
  <div>
     <el-row :gutter='20'>
            <!-- gutter表式行间距 -->
     </el-row>
        <!-- span 是列栅格  -->
    <el-row :gutter='20'>
        <el-col :span='6' :offset='8'>
          <el-form label-width="150px" > 
            <el-form-item label="用户名 :" >  
                <el-input v-model='username' type="text"/>
            </el-form-item>
            <el-form-item label="密码 :" >
                <el-input v-model='password' type="password"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
    </el-row>
  </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
export default {
  name: 'Login',
  setup () {
    const data = reactive({
      username: '',
      password: ''
    })
    async function onSubmit () {         //这行报错  Uncaught (in promise) TypeError: this is undefined, 格式错了吗?
      await this.$http.post('login', { username: data.username, password: data.password }, { emulateJSON: true })
    }
    return { ...toRefs(data), onSubmit }
  }
}
</script>

另外 element-plus 和 vue3.0 是不是兼容有问题? 有些莫名其妙的 BUG

  • 写回答

2条回答 默认 最新

  • been_ss 2021-10-18 11:08
    关注

    哈喽,vue3中的this应该按这种写法获取

    import {  getCurrentInstance } from 'vue'
    export default {
      setup () {
        const {proxy} = getCurrentInstance()
        // proxy等同于vue2的this
        return { }
      }
    }
    

    所以代码可以改成如下:

    import { reactive, toRefs, getCurrentInstance } from 'vue'
    export default {
      name: 'Login',
      setup () {
        const {proxy} = getCurrentInstance()
        const data = reactive({
          username: '',
          password: ''
        })
        async function onSubmit () {         
          await proxy.$http.post('login', { username: data.username, password: data.password }, { emulateJSON: true })
        }
        return { ...toRefs(data), onSubmit }
      }
    }
    

    不过,建议vue3直接用引入的方式去使用公共方法,不使用$挂全局方法

    import { 函数 }  from '模块地址'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月17日

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码