Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of using Vue in the backend #102

Merged
merged 10 commits into from
Nov 7, 2022
65 changes: 64 additions & 1 deletion console-asset-compilation.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
- [Registering theme packages](#registering-themes)
- [Mix configuration](#mix-configuration)
- [Examples](#examples)
- [Tailwind CSS](#examples-tailwind)
- [Vue JS](#examples-vue)
- [Commands](#commands)
- [Install Node dependencies](#mix-install)
- [Update Node dependencies](#mix-update)
@@ -118,6 +120,7 @@ When the `winter.mix.js` file is evaluated, regardless of where you ran `mix:com

Here are some examples of installing common frontend libraries for use with the asset compilation.

<a name="examples-tailwind"></a>
### Tailwind CSS

For themes that wish to use Tailwind CSS, include the `tailwindcss`, `postcss` and `autoprefixer` dependencies in your `package.json` file.
@@ -149,6 +152,66 @@ In the example above, we have a base CSS file that contains the Tailwind styling

Your theme will now be ready for Tailwind CSS development.

<a name="examples-vue"></a>
### Vue in a backend controller

If you want to use Vue 3 in your plugin for backend controllers, you can follow these steps.

```bash
# Inside the project root folder
npm install --save vue@latest && npm install --save-dev vue-loader@latest
```

Then, add a `winter.mix.js` configuration file to your plugin directory:

```js
const mix = require('laravel-mix');
mix
// compile javascript assets for plugin
.js('assets/src/js/app.js', 'assets/js').vue({ version: 3 })
```

Next you can create your Vue source files in your plugin's assets/src/js/ directory:

```js
// assets/src/js/app.js

import { createApp } from 'vue'
import Welcome from './components/Welcome'

const app = createApp({})

app.component('welcome', Welcome)

app.mount('#app')
```

```js
// assets/src/js/components/Welcome.vue

<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
setup: () => ({
title: 'Vue 3 in Laravel'
})
}
</script>
```
Now if you comple your assets in the project root with `mix:compile` then mix will create the file in your plugin under assets/js/app.js which includes Vue and all other packages that you use in your components.

Next in the your controller's template file (eg. controllers/myvuecontroller/index.php) you can inclue this generated app.js, and render the content in the div#app:

```
<div id="app">
<welcome/>
</div>

<script src="/plugins/foo/bar/assets/js/app.js"></script>
```

<a name="commands"></a>
## Commands

@@ -245,4 +308,4 @@ This can be useful for running arbitrary scripts that augment the capabilities o

By default, all package scripts are run in "development" mode. If you wish to run a script in "production" mode, add the `-f` or `--production` flag to the command.

If you wish to pass extra arguments or options to your script, you can add `--` to the end of the command. Any arguments or options added after the `--` argument are interpreted as arguments and options to be sent to the script itself.
If you wish to pass extra arguments or options to your script, you can add `--` to the end of the command. Any arguments or options added after the `--` argument are interpreted as arguments and options to be sent to the script itself.