Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将具有特定ID的数据传递给后端路由参数?_Javascript_Vue.js_Vuejs2_Vue Component_Vuex - Fatal编程技术网

Javascript 如何将具有特定ID的数据传递给后端路由参数?

Javascript 如何将具有特定ID的数据传递给后端路由参数?,javascript,vue.js,vuejs2,vue-component,vuex,Javascript,Vue.js,Vuejs2,Vue Component,Vuex,我想用当前id向后端路由参数发布消息。 如何让系统知道我正在传递此id Vuex行动: postMessage({commit}, payload, id) { axios.post(`http://localhost:5000/channels/${id}/messages` ,payload) .then((res) => { commit(SET_POSTS, res.data) }) } 这是张贴的行动,但我需要通过目前的渠道id不知何故。但是通道

我想用当前id向后端路由参数发布消息。 如何让系统知道我正在传递此id

Vuex行动:

postMessage({commit}, payload, id) {
  axios.post(`http://localhost:5000/channels/${id}/messages` ,payload)
    .then((res) => {
      commit(SET_POSTS, res.data)
    })
}
这是张贴的行动,但我需要通过目前的渠道id不知何故。但是通道ID在另一个组件中

postMessage() {
  const postData = {
    description: this.description,
    timestamp: this.timestamp
  };
  this.$store.dispatch("postMessage", postData)
},
在另一个组件中,我的侧菜单中有一个频道列表,比如discord,我这样显示它

 p.d-flex.align-items-center.channel-item(v-for="channel in channelName" :key="channel.id")
      strong.hash.mr-3 #
      | {{channel.channel_name}}

Vuex的主要优点之一是能够在一个组件中设置状态并从另一个组件获取状态。在另一个组件中,设置一些状态,如
state.id
。然后,您可以将该id传递给操作,也可以从操作内部的
状态获取该id

下面是一个传递它的示例:

方法

postMessage(){
常量postData={
id:this.$store.state.id,
description:this.description,
timestamp:this.timestamp
};
此.$store.dispatch(“postMessage”,postData)
}
Vuex操作始终仅提供2个参数,一个用于上下文对象(包含
提交
分派
等)和负载。将您的操作更改为:

行动

postMessage({commit},有效负载){
轴心柱(`http://localhost:5000/channels/${payload.id}/messages`,payload)
。然后((res)=>{
提交(设置_POSTS,res.data)
})
}
如果愿意,您可以使用payload参数并使用将
id
与其余部分分开:

行动

postMessage({commit},{id,…payload}){
轴心柱(`http://localhost:5000/channels/${id}/messages`,有效负载)
。然后((res)=>{
提交(设置_POSTS,res.data)
})
}
您还可以将id保留在有效负载之外,并直接从操作中的状态获取它:

行动

postMessage({state,commit},payload){
轴心柱(`http://localhost:5000/channels/${state.id}/messages`,有效负载)
。然后((res)=>{
提交(设置_POSTS,res.data)
})
}