【问题标题】:axios call return same value after props value changed道具值改变后axios调用返回相同的值
【发布时间】:2021-06-24 19:44:16
【问题描述】:

我的代码是这样的

子组件.vue

<script>

export default {
props: ['Pconvs_id'],
data(){
    return{
        user : '',
        messages:[],
        newMessage : '',
        convs_id: '',
        
    }
},
created(){
    this.convs_id = this.Pconvs_id;
    this.fetchMessages();
    
},
methods:
{
    fetchMessages()
    {
        console.log(this.convs_id);
        axios.get('messages',{cons_id: this.convs_id}).then(response=> {
            this.messages = response.data;
        });
        axios.get('authuser').then(response=>{
            this.user = response.data;
        });
    },
    
    
},
watch: {
// whenever convs_id changes, this function will run
Pconvs_id: function (newConvs_id, oldConvs_id) {
  this.convs_id = newConvs_id;
  this.fetchMessages();
}},}
</script>

父组件.vue

<Message :Pconvs_id="convs_id"/>

我的问题是即使 convs_id 改变了 fetchmessage() 返回相同的数据我做错了什么

axios 调用处理程序

public function fetchmessages(Request $cons_id)
{
    
    return Message::where([
       ['cons_id' , $cons_id],
    ])->with('user')->get();
   
}

等式关系 消息模型 beongsTo user 和 User hasMany message

【问题讨论】:

  • 我不确定您的语法是否需要第二个元素作为运算符。如果您使用正常的 where 语句,它会起作用吗? where('cons_id', $cons_id)

标签: laravel vuejs3 inertiajs


【解决方案1】:

我认为您的后端处理程序收到了Illuminate\Http\Request 的实例 所以要获得cons_ids,你应该像

public function fetchmessages(Request $request)
{
    // Get the cons_id from the request object
    $cons_id = $request->get('cons_id'); 

    // Cleaned up your "where" query
    return Message::where('cons_id', $cons_id)->with('user')->get(); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 2021-07-31
    • 2018-07-17
    • 2021-12-23
    • 2019-05-02
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多