首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel websockets聊天系统

Laravel websockets聊天系统
EN

Stack Overflow用户
提问于 2020-09-11 05:29:11
回答 1查看 398关注 0票数 0

嗨,我正试着建立一个laravel实时聊天。我配置了websockets,它似乎正在连接,但现在我试图发送一个消息不起作用。我在youtube上跟随一个教程,做了所有类似于教程中的事情,我不知道为什么我会收到这个错误。如果你能看一看,我会很感激的。

我所收到的错误

代码语言:javascript
复制
DevTools failed to load SourceMap: Could not load content for http://127.0.0.1:8000/js/popper.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
app.js:38405 [Vue warn]: Property or method "sendMessage" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Chats> at resources/js/components/ChatsComponent.vue
       <Root>
warn @ app.js:38405
warnNonPresent @ app.js:39818
get @ app.js:39873
keyup @ app.js:37616
invokeWithErrorHandling @ app.js:39634
invoker @ app.js:39959
original._wrapper @ app.js:45318
app.js:38405 [Vue warn]: Error in v-on handler: "TypeError: _vm.sendMessage is not a function"

found in

---> <Chats> at resources/js/components/ChatsComponent.vue
       <Root>
warn @ app.js:38405
logError @ app.js:39664
globalHandleError @ app.js:39659
handleError @ app.js:39619
invokeWithErrorHandling @ app.js:39642
invoker @ app.js:39959
original._wrapper @ app.js:45318
app.js:39668 TypeError: _vm.sendMessage is not a function
    at keyup (app.js:37616)
    at invokeWithErrorHandling (app.js:39634)
    at HTMLInputElement.invoker (app.js:39959)
    at HTMLInputElement.original._wrapper (app.js:45318)
logError @ app.js:39668
globalHandleError @ app.js:39659
handleError @ app.js:39619
invokeWithErrorHandling @ app.js:39642
invoker @ app.js:39959
original._wrapper @ app.js:45318
app.js:38405 [Vue warn]: Property or method "sendMessage" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

我在user类中创建了这个函数。

代码语言:javascript
复制
 public function messages()
    {
        return $this->hasMany(Message::class);
    }

消息类

代码语言:javascript
复制
class Message extends Model
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $fillable = ['message'];
    public function user()
    {
        return $this->belognsTo(User::class);
    }
}

控制器

使用照明\Http\请求;使用App\Message;

代码语言:javascript
复制
class ChatsController extends Controller
{
    public function _construct(){
        $this->middleware('auth');
    }
    
    public function index()
    {
        return view('chats');
    }

    public function fetchMessages()
    {
        return Message::with('user')->get();
    }

    public function sendMessages(Request $request)
    {
       $message = auth()->user()->messages()->create([
            'message' => $request->message
        ]);

        return ['status' =>'success'];
    }
}

Vue chatsComponent

代码语言:javascript
复制
<template>
    <div class="row">
        <div class="col-8">
            <div class="card card-default">
                <div class="card-header">Messages</div>
                <div class="card-body p-0">
                    <ul class="list-unstyled" style="height:300px; overflow-y:scroll">
                        <li class="p-2" v-for="(message,index) in messages" :key="index">
                            <strong>{{ message.user.name }}</strong>
                            {{ message.message }}
                        </li>
                    </ul>
                </div>

                <input
                    @keyup.enter="sendMessage"
                    v-model="newMessage"
                    type="text" 
                    name="message" 
                    placeholder="Enter your message..."
                    class="form-control">
            </div>
             <span class="text-muted">User is typing...</span>
        </div>

        <div class="col-4">
            <div class="card card-default">
                <div class="card-header">Active Users</div>
                <div class="card-body">
                    <ul>
                        <li class="py-2">Harish</li>
                    </ul>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
    export default {

        props:['user'],

        data(){
            return {
                messages:[],
                newMessage:'',
            }
        },

        created(){
           this.fetchMessages();
        },

        methods:{
            fetchMessages(){
              axios.get('messages').then(response =>{
                this.messages = response.data
                })                
            },

            sendMessages(){
                this.messages.push({
                    user:this.user,
                    message:this.newMessage
                });
                
                axios.post('messages', {message: this.newMessage});

                this.newMessage='';
            }
        }
    }
</script>

聊天视图

代码语言:javascript
复制
@extends('layouts.app')

@section('content')
<div class="container">
    <chats :user="{{ auth()->user()}}"></chats>
</div>
@endsection

路由

代码语言:javascript
复制
Route::get('/chats','ChatsController@index')->middleware('auth');
Route::get('/messages','ChatsController@fetchMessages');
Route::post('/messages','ChatsController@sendMessages');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-11 05:31:53

@keyup.enter="sendMessage"更改为@keyup.enter="sendMessages"

s在这里失踪了

这就是为什么你得到了Property or method "sendMessage" is not defined

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63841245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档