Laravel VueJs component inline template, unknown custom element

老子叫甜甜 提交于 2019-12-02 14:13:38

When registering a Vue component, you need to include a name, list below:

export default {
        name: 'example-name',
        data() {
            return {
            }
        },

This should help in your situation - let me know :)

I finally found the solution to use VueJS inline template in Laravel.

Using this solution, you can enjoy the best of both world, you can use familiar Laravel blade syntax while keep your application reactive using VueJS.

This technique also covered in Laracast episode https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/32

Here is the complete step by step

Step 1: Create a Single File Component. Example ShowUserComponent.vue

<script>

        export default {
            mounted() {
                console.log('inline template component');
            },
            data () {
                return {
                    message: 'This is test message'
                }
            },
            methods: {
                store() {
                    alert('Test inline component method')
                }
            }
        }
    </script>

Step 2: Reference the file in Laravel app.js and run 'npm run dev' command in terminal

Vue.component('show-user-component', require('./components/admin/users/ShowUserComponent.vue'));

Step 3: Now you can use the component inside Laravel Blade file

@extends('layouts.app')

    @section('content')

        <div class="content">
            <show-user-component inline-template>
                        <div>
                            <h1>@lang('users.edit')</h1>
                            <input type="text" v-model="message">
                            <button @click="store" >Click</button>
                        </div>
                    </show-user-component>
        </div>

    @endsection
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!