Javascript 在vue中执行嵌套函数

Javascript 在vue中执行嵌套函数,javascript,vue.js,Javascript,Vue.js,我有两个嵌套在vue中的函数,父函数应该获取属性值,而子函数应该使用属性值进行api调用。如何执行此函数一次以确保获得此属性并立即进行api调用 //button with the attribute I want <button :data-post-id="My id">Click Me</button> //Here I'm calling the parent function <button @click="getPos

我有两个嵌套在vue中的函数,父函数应该获取属性值,而子函数应该使用属性值进行api调用。如何执行此函数一次以确保获得此属性并立即进行api调用

   //button with the attribute I want
    <button :data-post-id="My id">Click Me</button>
   //Here I'm calling the parent function
        <button @click="getPostId">Submit to api</button> 

我不是vue专家,但我能发现一个不一致之处

您正在将回调绑定到子对象,但在父对象上设置attr
数据post id
,然后希望子对象具有该attr。此外,属性名称似乎不匹配,即设置的内容和尝试获取的内容


至于最初的问题,我不确定为什么不将属性也添加到子元素中,如果不能这样做,则需要通过DOM找到所需的父元素

您的方法将多次创建函数,只需从简单函数开始,并保持独立

newvue({
el:“#应用程序”,
数据:{
发布ID:'
},
方法:{
setPostId:函数(id){
this.postid=id;
},
getPostId:函数(){
console.log(this.posted);
}
}
})

第11组
第22集
第33组
发帖
{{postid}}

@mots您可以执行以下操作

usePostId: function(id) {
  console.log("I am accessible here " + id)
},

getPostId: function(evt) {
  const postId = evt.target.getAttribute('data-post-id')
  const output = this.usePostId(postId)
  return output
}

你可能只是试图解决你的问题,但有些东西正在丢失。在这里重新创建您的问题codesandbox.io/s/vue
usePostId: function(id) {
  console.log("I am accessible here " + id)
},

getPostId: function(evt) {
  const postId = evt.target.getAttribute('data-post-id')
  const output = this.usePostId(postId)
  return output
}