Cara Mudah Install Vue Template Pada Laravel

1. Menginstall Depedency

Di sini kita perlu menginstall depedency pada laravel untuk menghubungkan dengan ui Vue nantinya.

composer require laravel/ui

Kemudian install Vue.

php artisan ui vue
npm install

2. Membuat Component

Pada langkah ini kita akan membuat contoh component baru pada resources\js\components\Attachment.vue.

<template>
    <div>
        <div>
            <div v-for="attachment in dataAttachments" v-bind:key="attachment.id" class="mb-3">
                <div>
                    <a :href="attachment.full_url" target="_blank"></a>
                    <input type="hidden" name="update_attachment[]" :value="attachment.id"/>
                    <button type="button" class="btn btn-nooutline btn-nooutline-main"  onclick="this.parentNode.parentNode.removeChild(this.parentNode);"><i class="fas fa-times"></i></button>
               </div>
            </div>
            <div v-for="(attachment, index) in attachments" v-bind:key="index" class="mb-3">
                <div class="p-1 border d-inline-block">
                    <input type="file" name="attachments[]" accept=".pdf,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document" required>
                </div>
                <button type="button" class="btn btn-nooutline btn-nooutline-main" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"><i class="fas fa-times"></i></button>
            </div>
            <div>
                <button type="button" class="btn btn-outline btn-outline-main" v-on:click="addFieldAttach">Add More</button>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        props: {
            db: Array,
        },
        data() {
            return {
                attachments : [],
                dataAttachments: []
            }
        },
        mounted: function() {
            this.setAttachments()
        },
        methods: {
            setAttachments: function() {
                this.dataAttachments = this.db
            },
            addFieldAttach: function() {
                this.attachments.push({})
            },
            getFileName: function (str) {
                return new String(str).substring(str.lastIndexOf('/') + 1);
            }
        }
    }
</script>

Tambahkan component yang telah kita buat pada resources\js\app.js.

Vue.component('attachment', require('./components/Attachment.vue').default);

Panggil pada view blade.

<attachment :db="[]"></attachment>

Kemudian generate vue dengan menjalankan perintah.

Development

npm run watch/dev

Production

npm run prod

Catatan

Sekian untuk kali ini semoga bermanfaat :D untuk lebih lanjut bisa kunjungi link tersebut.