【问题标题】:can't initialized Vue in laravel 5.4无法在 laravel 5.4 中初始化 Vue
【发布时间】:2025-11-27 18:05:01
【问题描述】:

我无法在我的 Laravel 5.4 项目上运行一个简单的 Vue

我在 package.json 中有依赖项

 "devDependencies": {
    "axios": "^0.15.3",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^3.2.3",
    "jquery": "^3.1.1",
    "laravel-mix": "^0.8.3",
    "lodash": "^4.17.4",
    "vue": "^2.1.10"
  },

我将 app.js 放在 webpack.mix 上

let 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/assets/js/app.js', 'public/js');

在我的 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');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */


Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#contenedorPrincipal'
});

例子.vue

<template>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Example Component</div>

                    <div class="panel-body">
                        I'm an example component!
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

在我看来,我在布局中有一个 id = contenedorPrincipal 的 div,我在单个视图中调用示例..

app.blade.php

<div class="content-page" id="contenedorPrincipal">
    <div class="content">
        @yield('content')

    </div>

在 vie myview.blade.php 中

@extends('layouts.app')

@section('content')
<div>
   <example></example>
</div>
@stop

npm run watch 一直在工作....

当我打开这个视图时,什么都没有显示,没有错误,没有从 vue 检查器检测到 vue 组件......

我不知道是什么问题..

【问题讨论】:

  • 在您的 myview.blade.php 中,您是否包装了您想用@section('content')@endsection 填充yield 槽的HTMl?
  • 您还可以通过检查 Vue 是否存在于您的开发控制台内的窗口中来仔细检查 Vue 是否真的被引导到窗口中。

标签: php laravel vue.js


【解决方案1】:

已修复,我忘记在我的 app.blade.php 中

<script src="{{ asset('js/app.js') }}"></script>

谢谢大家!

【讨论】: