How to setup inertia on my project, gives me an error when I try and load the login page

Nancy picture Nancy · Sep 27, 2019 · Viewed 9.2k times · Source

I'm trying to setup Inertia to use in my Laravel project but it gives me errors? Where is my mistake?

I installed Inertia with this command composer require inertiajs/inertia-laravel

followed the instructions on the github page and added @inertia to my app.blade.php like this:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
    <link rel="icon" type="image/jpg" href="{{asset("/image/logo2.png")}}">
    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>

@inertia

</body>
</html>

in my Login Controller

 public function showLoginForm()
    {
        return Inertia::render('Auth/Login');
    }

in my routes/web.php

Auth::routes();
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');

This is the error I get:

the lines that is highlighted is the @inertia which show up as this
<div id="app" data-page="<?php echo e(json_encode($page)); ?>"></div>

What am i doing wrong?

Answer

Salim Djerbouh picture Salim Djerbouh · Sep 27, 2019

@inertia blade directive is working but not rendered because you need to install the frontend adapter

npm install @inertiajs/inertia @inertiajs/inertia-vue

Set it up in webpack.mix.js 

const mix = require('laravel-mix')
const path = require('path')

mix.js('resources/js/app.js', 'public/js')
  .webpackConfig({
    output: { chunkFilename: 'js/[name].js?id=[chunkhash]' },
    resolve: {
      alias: {
        vue$: 'vue/dist/vue.runtime.esm.js',
        '@': path.resolve('resources/js'),
      },
    },
  })

And initialize it in Vue resources/js/app.js

import { InertiaApp } from '@inertiajs/inertia-vue'
import Vue from 'vue'

Vue.use(InertiaApp)

const app = document.getElementById('app')

const pages = {
  'Auth/Login': require('./Pages/Auth/Login.vue').default,
}

new Vue({
  render: h => h(InertiaApp, {
    props: {
      initialPage: JSON.parse(app.dataset.page),
      resolveComponent: name => pages[name],
    },
  }),
}).$mount(app)