繁体   English   中英

Laravel 5.8和Vue.js组件

[英]Laravel 5.8 and Vue.js components

我是使用Vue.js的新手,我正在尝试学习如何使用vue-star-rating在我的Laravel应用程序中实现食谱评级系统。 我想要学习的第一件事是如何构建组件,然后将它们包含在我的刀片视图中。 我已经遇到了麻烦。 试图包含开箱即用的Laravel附带的ExampleComponent.vue,但我不明白我是如何将它包含在我的刀片文件中的。 我的刀片文件的顶部看起来像这样

刀锋视图

<div class="col-sm-6">
                <div class="container recipeDesc">
                    <h2 class="text-center name">{{$recipe->title}}</h2>
                        <div class="stars">
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                        </div>

                        <div id="app">
                            <example-component></example-component>
                        </div>

默认的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');

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

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

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * 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.
 */

const app = new Vue({
    el: '#app',
    components: {
        'example-component': require('./components/ExampleComponent.vue'),
    }
});

我如何在这个div中包含ExampleComponent.vue? 即使在阅读Vue.js文档时也很难理解这一点。 谢谢!

如果您仍然拥有Laravel安装的默认js文件,那么在resources / js / app.js文件中,您应该具有以下内容:

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

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

第一行告诉您,示例组件应使用的名称是example-component,因此要将其包含在刀片视图中,只需写下:

<example-component/>

app.js文件中的第二个块告诉您Vue将处理id =“app”的元素,因此如果要使用Vue组件,则应确保在布局上有一个具有该id的div。

在正文结束标记之前,您应该包含vue脚本源,以便能够在您的视图中实现它: <script src="{{ asset('js/app.js') }}"></script></body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM