|
| 1 | +Laravel Blade Vue Directive |
| 2 | +============== |
| 3 | + |
| 4 | +Laravel Blade Vue Directive provides a blade directive for vue.js inline components. |
| 5 | + |
| 6 | +[](https://packagist.org/packages/jhoff/blade-vue-directive) |
| 7 | +[](https://packagist.org/packages/jhoff/blade-vue-directive) |
| 8 | +[](https://packagist.org/packages/jhoff/blade-vue-directive) |
| 9 | +[](https://scrutinizer-ci.com/g/jhoff/blade-vue-directive/build-status/master) |
| 10 | +[](https://scrutinizer-ci.com/g/jhoff/blade-vue-directive/?branch=master) |
| 11 | +[](https://scrutinizer-ci.com/g/jhoff/blade-vue-directive/?branch=master) |
| 12 | + |
| 13 | +<!-- MarkdownTOC autolink="true" autoanchor="true" bracket="round" depth="4" --> |
| 14 | + |
| 15 | +- [Installation](#installation) |
| 16 | +- [Usage](#usage) |
| 17 | + - [Basic Example](#basic-example) |
| 18 | + - [Scalars Example](#scalars-example) |
| 19 | + - [Booleans and Numbers Example](#booleans-and-numbers-example) |
| 20 | + - [Objects and Arrays Example](#objects-and-arrays-example) |
| 21 | + |
| 22 | +<!-- /MarkdownTOC --> |
| 23 | + |
| 24 | +<a name="installation"></a> |
| 25 | +## Installation |
| 26 | + |
| 27 | +To install the Laravel Blade Vue Directive, simply run `composer require blade-vue-directive` in a terminal, or if you prefer to manually install you can the following in your `composer.json` file and then run `composer install` from the terminal: |
| 28 | + |
| 29 | +```json |
| 30 | +{ |
| 31 | + "require": { |
| 32 | + "jhoff/blade-vue-directive": "1.*" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Then all you need to do is add the new provider to the providers array of `config/app.php`: |
| 38 | + |
| 39 | +```php |
| 40 | + 'providers' => [ |
| 41 | + // ... |
| 42 | + Jhoff\BladeVue\DirectiveServiceProvider::class, |
| 43 | + // ... |
| 44 | + ], |
| 45 | +``` |
| 46 | + |
| 47 | +<a name="usage"></a> |
| 48 | +## Usage |
| 49 | + |
| 50 | +The Laravel Blade Vue Directive was written to be simple and intuitive to use. It's not opinionated about how you use your vue.js components. Simply provide a component name and (optionally) an associative array of properties. |
| 51 | + |
| 52 | +<a name="basic-example"></a> |
| 53 | +### Basic Example |
| 54 | + |
| 55 | +Using the vue directive with no arguments in your blade file |
| 56 | + |
| 57 | +```html |
| 58 | + @vue('my-component') |
| 59 | + <div>Some content</div> |
| 60 | + @endvue |
| 61 | +``` |
| 62 | + |
| 63 | +Renders in html as |
| 64 | + |
| 65 | +```html |
| 66 | + <component inline-template v-cloak is="my-component"> |
| 67 | + <div>Some content</div> |
| 68 | + </component> |
| 69 | +``` |
| 70 | + |
| 71 | +<a name="scalars-example"></a> |
| 72 | +### Scalars Example |
| 73 | + |
| 74 | +Using the vue directive with an associative array as the second argument will automatically translate into native properties that you can use within your vue.js components. |
| 75 | + |
| 76 | +```html |
| 77 | + @vue('page-title', ['title' => 'Welcome to my page']) |
| 78 | + <h1>@{{ title }}</h1> |
| 79 | + @endvue |
| 80 | +``` |
| 81 | + |
| 82 | +Renders in html as |
| 83 | + |
| 84 | +```html |
| 85 | + <component inline-template v-cloak is="page-title" title="Welcome to my page"> |
| 86 | + <h1>{{ title }}</h1> |
| 87 | + </component> |
| 88 | +``` |
| 89 | + |
| 90 | +Then, to use the properties in your vue.js component, add them to `props` in your component definition. See [Component::props](https://vuejs.org/v2/guide/components.html#Props) for more information. |
| 91 | + |
| 92 | +```js |
| 93 | + Vue.component('page-title', { |
| 94 | + props: ['title'] |
| 95 | + }); |
| 96 | +``` |
| 97 | + |
| 98 | +<a name="booleans-and-numbers-example"></a> |
| 99 | +### Booleans and Numbers Example |
| 100 | + |
| 101 | +Properties that are booleans or numeric will be bound automatically as well |
| 102 | + |
| 103 | +```html |
| 104 | + @vue('report-viewer', ['show' => true, 'report' => 8675309]) |
| 105 | + <h1 v-if="show">Report #@{{ report }}</h1> |
| 106 | + @endvue |
| 107 | +``` |
| 108 | + |
| 109 | +Renders in html as |
| 110 | + |
| 111 | +```html |
| 112 | + <component inline-template v-cloak is="report-viewer" :show="true" :report="8675309"> |
| 113 | + <h1 v-if="show">Report #{{ report }}</h1> |
| 114 | + </component> |
| 115 | +``` |
| 116 | + |
| 117 | +<a name="objects-and-arrays-example"></a> |
| 118 | +### Objects and Arrays Example |
| 119 | + |
| 120 | +The vue directive will automatically handle any objects or arrays to make sure that vue.js can interact with them without any additional effort. |
| 121 | + |
| 122 | +```html |
| 123 | + @vue('welcome-header', ['user' => (object)['name' => 'Jordan Hoff']]) |
| 124 | + <h2>Welcome @{{ user.name }}!</h2> |
| 125 | + @endvue |
| 126 | +``` |
| 127 | + |
| 128 | +Renders in html as |
| 129 | + |
| 130 | +```html |
| 131 | + <component inline-template v-cloak is="welcome-header" :user="{"name":"Jordan Hoff"}"> |
| 132 | + <h2>Welcome {{ user.name }}!</h2> |
| 133 | + </component> |
| 134 | +``` |
| 135 | + |
| 136 | +Notice that the object is json encoded, html escaped and the property is prepended with `:` to ensure that vue will bind the value as data. |
| 137 | + |
| 138 | +To use an object property in your component, make sure to make it an `Object` type: |
| 139 | + |
| 140 | +```js |
| 141 | + Vue.component('welcome-header', { |
| 142 | + props: { |
| 143 | + user: { |
| 144 | + type: Object |
| 145 | + } |
| 146 | + } |
| 147 | + }); |
| 148 | +``` |
0 commit comments