首页 > 解决方案 > 停止在 Laravel 中渲染 Vue 组件

问题描述

我有新安装的 Laravel 应用程序也安装了 npm,现在我尝试加载此文件的每个页面都会加载ExampleComponent.vue

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-component', require('./components/ExampleComponent.vue'));

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

ExampleComponent.vue

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

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

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

问题

由于我没有使用 VueJs 的计划,而且我还需要查看我创建的实际页面,我如何阻止 VueJs在我的应用程序中呈现?

标签: laravelvue.js

解决方案


app.js引导程序包含后删除所有内容,然后使用npm run dev.

然而,默认情况下,Laravel 对 Vuejs 不做任何事情。因为它在带有 id 的页面上没有 html 元素#app。检查您的resources/views目录,默认刀片文件是welcome.blade.php.

确切地说,流程:

  • 您的路由文件指示如何处理请求以及到哪个控制器
  • 负责路由的控制器将(可能)提供刀片视图
  • 刀片视图将被解析,如果它有一个#app元素并将app.jsVue 绑定到#appVue 将接管。

https://github.com/laravel/laravel/blob/master/resources/views/welcome.blade.php


推荐阅读