【问题标题】:Laravel 5.8 and Vue.js componentsLaravel 5.8 和 Vue.js 组件
【发布时间】:2019-09-16 22:18:59
【问题描述】:

我是使用 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 文档,也很难理解这一点。 谢谢!

【问题讨论】:

    标签: javascript laravel vue.js


    【解决方案1】:

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

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

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

    &lt;example-component/&gt;

    app.js 文件中的第二个块告诉你 Vue 将在一个 id="app" 的元素上工作,所以如果你想使用 Vue,你应该确保你的布局中有一个具有该 id 的 div组件。

    【讨论】:

    • 我必须在刀片布局文件中包含
      还是将其包含在要包含 Vue 组件的特定刀片视图中?
    • 我总是把它放在总体布局上,这样我就可以在我的所有视图中使用 Vue,以备不时之需。为了清楚起见,我忘了说你应该编译你的 js 并包含它。
    • 即使我这样做了,我也没有让示例组件工作。在我看来,它没有呈现。什么都没有发生。也没有错误。它似乎不起作用。我想在 ExampleComponent.vue 中呈现 HTML 标记
    • 它说“找不到 vue-star-rating 的声明文件”: var StarRating = require('vue-star-rating');
    【解决方案2】:

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

    【讨论】:

    • 如果我按照你说的做,我可以在没有php artisan serve 命令的情况下运行laravel应用吗?
    • @IstiaqueAhmed 您仍然需要为您的应用提供服务,因此还需要它
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签