2017 © Pedro Peláez
 

library laravel-vue-component

Helper package to aid usage of Vue Components within Laravel projects

image

riverskies/laravel-vue-component

Helper package to aid usage of Vue Components within Laravel projects

  • Saturday, February 24, 2018
  • by barnabaskecskes
  • Repository
  • 1 Watchers
  • 14 Stars
  • 721 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 8 Versions
  • 77 % Grown

The README.md

Laravel Vue Component

A helper package to aid working with Vue Components in a (non-SPA) Laravel environment. This package makes injecting page-specific Vue Components a breeze., (*1)

Sometimes we come across situations where the project is not using Vue extensively, but some pages could highly benefit from it being present. This package aims at those scenarios and makes it possible to (optionally) wrap pages with dedicated Vue components directly from your PHP backend., (*2)

When would you use this package?

When building a multi-page application there is often a need to inject some Vue magic into specific pages. However, more often then not, as the project grows, the individual script tags get out of control and become very hard to manage., (*3)

How does this package work?

The package implements a new Blade directive that you can safely use to wrap your yielded content in your Blade templates. By using this new directive you are able to inject a custom, page-specific Vue component into any of your pages while keeping your scripts modular therefore manageable at the same time., (*4)

Installation

NOTE - For those with Laravel 5.2 or before: please use v1.0.2 instead!!!, (*5)

Install the package through composer:, (*6)

$ composer require riverskies/laravel-vue-component

If you're running Laravel 5.4 or earlier, add the service provider to your config/app.php file:, (*7)

Riverskies\Laravel\VueComponent\VueComponentServiceProvider::class,

Usage

Wrap your yield block in your layout file with the new directive:, (*8)

<div id="app">
    @vue($component)
        @yield('content')
    @endvue
</div>

If you have $component variable set on your view..., (*9)

return view('pages.home', ['component' => 'homepage']);

...the content will be wrapped within a dynamic Vue component:, (*10)

<component is="homepage" inline-template v-cloak>
    <!-- yielded content -->
</component>

To inject the homepage component dynamically, you will need to have the set component (homepage) available on your root Vue instance. For example, your app.js file might look like this:, (*11)

import Homepage from './pages/homepage.js';

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

Examples

1. Simple page injection

An example where you connect a blade template to a Vue page instance. Consider following scenario..., (*12)

In your controller method:, (*13)

return view('pages.contact', ['component' => 'contact']);

In your pages.contact.blade file (note the wrapping <div> and the escaped @{{ ... }} syntax):, (*14)

@section('content')
    <div class='contact-page'>
        <h1>Contact us</h1>

        <ul class="errors" v-if="errors.length > 0">
            <li v-for="error in errors">@{{ error }}</li>
        </ul>

        <form @submit.prevent='submitForm'>
            <input v-model="email"/>
            <textarea v-model="message">@{{ message }}</textarea>
            <button type='submit'>Submit</button>
        </form>
    </div>
@endsection

In your pages/contact.js file:, (*15)

export default {
    data() {
        return {
            email: '',
            message: '',
            errors: []
        }
    },
    methods: {
        submitForm() {
            // your code to verify/send form data
        }
    }
}

And your main app.js file to be like this:, (*16)



import Contact from './pages/contact.js'; const app = new Vue({ el: '#app', components: { Contact } });

With above, you are successfully injected VueJS control into your contact page., (*17)

2. Sending through data

Based on Example 1, you can send through data from the backend directly as well. Consider following scenario..., (*18)

In your controller method:, (*19)

// in your controller method
return view('welcome', [
    'component' => [
        'is' => 'contact',
        'data' => [
            'email' => 'john@example.com'
        ]
    ]
]);

In your pages/contact.js file:, (*20)

export default {
    props: ['data'],
    data() {
        return {
            email: '',
            message: '',
            errors: [],
            ssData: null
        }
    },
    created() {
        // you must evaluate passed through data!
        this.ssData = eval(this.data);

        this.email = this.ssData.email;
    },
    methods: {
        submitForm() {
            // your code to verify/send form data
        }
    }
}

This method can be used to send through data from the backend directly., (*21)

The Versions

24/02 2018

dev-master

9999999-dev

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

24/02 2018

v1.2

1.2.0.0

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

12/03 2017

v1.1

1.1.0.0

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

12/03 2017

dev-dev

dev-dev

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

12/03 2017

dev-crap

dev-crap

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

14/05 2016

1.0.2

1.0.2.0

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

14/05 2016

1.0.1

1.0.1.0

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component

14/05 2016

1.0.0

1.0.0.0

Helper package to aid usage of Vue Components within Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barnabas Kecskes

laravel component vue vue-component