【问题标题】:Vuejs/Posgres - When clicked on button I want to save a value in db postgresqlVuejs/Posgres - 单击按钮时,我想在 db postgresql 中保存一个值
【发布时间】:2021-08-11 17:11:44
【问题描述】:

嗨,所以我有一个视图,其中有一个按钮,单击它时,我希望将一个值保存在 db 中。我现在得到的和我点击按钮没什么两样,但什么也没有发生。 这是我的代码:

<a-button type="primary" class="mb-4 text-center mr-1 float-right"  @click="onSubmit">Confirm</a-button>

在我的脚本中:

   setup(){
const onSubmit = () => {
  
axios.post("/insertstatut/"+876,"added").then((res)=>{
    message.success(`statut ajouté`)
    router.push({ 
          path:'/cand',
        }).catch(error => {
      console.log('error', error);
    })
  } ,
  )
  }
  }

如果你知道我应该做什么,请分享谢谢。

【问题讨论】:

  • 您看到哪些浏览器控制台错误?
  • 我没有收到错误,我实际上认为即使从查询中也一切都错了,我完全迷失了我不知道从哪里开始或应该有什么
  • 您单独显示的代码应该会产生错误,因为它不会返回onSubmit。你能链接到重现问题的代码框吗?
  • 您确定您的问题与您的服务器端无关吗?
  • 您没有返回setup 中的onSubmit 方法。在setup的末尾做return {onSubmit}

标签: postgresql vue.js axios vuejs3


【解决方案1】:

您在 vue 代码中使用了组合 API 功能设置, 您需要返回您希望在模板中使用的方法或属性。

setup () {
   return {
     onSubmit: () => {}, //some method u want to use later in template ,
     show: false, // or some property 
   }
}

【讨论】:

    【解决方案2】:

    这就是您的组件的外观

    <template>
      <a-button
        type="primary"
        class="mb-4
        text-center
        mr-1float-right"
        @click="onSubmit"
      >
        Confirm
      </a-button>
    </template>
    <script>
    import AButton from './button-path/AButton.vue'
    import axios from 'axios'
    export default {
      componets: { AButton },
      setup() {
        const onSubmit = () => {
          axios.post('/insertstatut/' + 876, 'added').then((res) => {
            message.success(`statut ajouté`)
            router
              .push({
                path: '/cand',
              })
              .catch((error) => {
                console.log('error', error)
              })
          })
        }
        // Expose your constants/methods/functions
        return {
          onSubmit,
        }
      },
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多