为什么Vue JS在我的laravel项目中不起作用

时间:2019-12-05 15:52:44

标签: javascript laravel vuejs2

我有一个带有一些自定义更改的laravel项目 我尝试使用Vue js代码,但在我的情况下不起作用 我已经运行了所有必需的命令,但是当我检查控制台并显示消息未定义时仍然无法正常工作。

我的webpack.config.js

 const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css');

我的app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');


window.Vue = require('vue');

import Vue from "vue";
//import Vue from 'vue/dist/vue.js';


// const files = require.context('./', true, /\.vue$/i);
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));


const app = new Vue({
    el: '#app',
    data:{
        message: '',

    },
    methods: {
        send()
        {
            console.log(this.message);
        }
    }
    //
});

我的布局/app.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Demo Laravel Vue Chat</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <!-- CSS Tags -->
    <link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/style.css') }}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway:400,500,500i,700,800i" rel="stylesheet">
</head>
<body>
    @include('partials.__nav')

    <div class="container-fluid">
        @yield('content')
    </div>

    @include('partials.__footer')


    <!-- JS Tags -->
    <script type="text/javascript" src="{{ asset('js/app.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/script.js') }}"></script>
</body>
</html>

我使用我在app.js(#app)中声明的ID的索引文件

@extends('layouts.app')
@section('content')

<div class="row my-lg-2" id="app">
    <div class="messaging">
      <div class="inbox_msg">
        <div class="inbox_people">
          <div class="headind_srch">
            <div class="recent_heading">
              <h4>My Messages <span class="text-danger">(5 New)</span></h4>
            </div>
            <div class="srch_bar">
              <div class="stylish-input-group">
                <input type="text" class="search-bar"  placeholder="Search Groups or Users" >
                <span class="input-group-addon">
                <button type="button"> <i class="fa fa-search" aria-hidden="true"></i> </button>
                </span> </div>
            </div>
          </div>
          <div class="inbox_chat">
            <div class="chat_list active_chat">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/todd.png') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Todd Ogryzlo<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/bipin.png') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Nepali Developers<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/victor.jpeg') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Victor<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/kostas.png') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Laravel PHP Canada<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/pratima.jpeg') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Pratima Sharma<span class="chat_date">Dec 25</span></h5>
                  <p>Test, which is a new approach to have all solutions 
                    astrology under one roof.</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/nishal.jpeg') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Nishal Gurung<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
            <div class="chat_list">
              <div class="chat_people">
                <div class="chat_img"> <img src="{{ asset('img/niraj.jpeg') }}" alt="sunil"> </div>
                <div class="chat_ib">
                  <h5>Niraj Pun Magar<span class="chat_date">Dec 25</span></h5>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,</p>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="mesgs">
            <h3 class="first_head">Todd</h3>
          <div class="msg_history">
            <div class="incoming_msg">
              <div class="incoming_msg_img"> <img src="{{ asset('img/todd.png') }}" alt="sunil"> </div>
              <div class="received_msg">
                <div class="received_withd_msg">
                  <p>Hi Bipin How Are you doing ?</p>
                  <span class="time_date"> 11:01 AM    |    June 9</span></div>
              </div>
            </div>
            <div class="outgoing_msg">
              <div class="sent_msg">
                <p>I am Good Todd . What about you</p>
                <span class="time_date"> 11:01 AM    |    June 9</span> </div>
            </div>
            <div class="incoming_msg">
              <div class="incoming_msg_img"> <img src="{{ asset('img/todd.png') }}" alt="sunil"> </div>
              <div class="received_msg">
                <div class="received_withd_msg">
                  <p>How is newgrad project going ?</p>
                  <span class="time_date"> 11:01 AM    |    Yesterday</span></div>
              </div>
            </div>
            <div class="outgoing_msg">
              <div class="sent_msg">
                <p>Going very good . Going to Finish it very soon</p>
                <span class="time_date"> 11:01 AM    |    Today</span> </div>
            </div>
            <div class="incoming_msg">
              <div class="incoming_msg_img"> <img src="{{ asset('img/todd.png') }}" alt="sunil"> </div>
              <div class="received_msg">
                <div class="received_withd_msg">
                  <p>Okkay Good ! When you finish let me and victor know about it</p>
                  <span class="time_date"> 11:01 AM    |    Today</span></div>
              </div>
            </div>
          </div>
          <div class="type_msg">
            <div class="input_msg_write" id="app">
              <input type="text" class="write_msg" placeholder="Type a message" v-model='message' />
              <button class="msg_send_btn" type="button"><i class="fa fa-paper-plane-o" aria-hidden="true"></i></button>
            </div>
          </div>
        </div>
      </div>
    </div>
</div>
@endsection

并且我在一次输入的索引文件底部使用了v-model 但它在控制台中显示错误,并且均未显示索引页面内容 这是我在控制台中遇到的错误

app.js:1 ReferenceError: message is not defined
    at hn.eval (eval at Sa (app.js:1), <anonymous>:1:424)
    at hn.e._render (app.js:1)
    at hn.r (app.js:1)
    at Nt.get (app.js:1)
    at new Nt (app.js:1)
    at app.js:1
    at hn.$mount (app.js:1)
    at hn.$mount (app.js:1)
    at hn.e._init (app.js:1)
    at new hn (app.js:1)
qe @ app.js:1
script.js:24 Uncaught TypeError: Cannot set property 'onclick' of null
    at script.js:24

2 个答案:

答案 0 :(得分:2)

您应该1)从错误开始:

app.js:1 ReferenceError: message is not defined
    at hn.eval (eval at Sa (app.js:1), <anonymous>:1:424)
    at hn.e._render (app.js:1)
    at hn.r (app.js:1)
    at Nt.get (app.js:1)
    ...

然后2)向后退:

require('./bootstrap');

window.Vue = require('vue');

import Vue from "vue";
//import Vue from 'vue/dist/vue.js';

// const files = require.context('./', true, /\.vue$/i);
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));

const app = new Vue({
    el: '#app',
    data: {
        message: '',
    },
    methods: {
        send() {
            console.log(this.message);  // <-- Do you have a "this.message" in app?
        }
    }
});

所以也许解决方法是console.log(this.data.message);,请尝试一下,然后将您发现的内容发回。

更重要的是,请熟悉JS调试工具,例如Visual Studio Code和/或Chrome Developer Tools

答案 1 :(得分:0)

嗯,我看到两个id='app'

<div class="row my-lg-2" id="app">


<div class="input_msg_write" id="app">

您删除第二个