1

After installing fresh installation of Laravel i installed VueRouter and after implementing simple code for that such as below, i can't use VueJs and i don't get any error in terminal:

commands don't show any error and i get build successful from Laravel mix /resources/js/app.js content:

GITHUB project link:

import Vue from "vue";
import VueRouter from 'vue-router';
import ExampleComponent from "./components/ExampleComponent";

require('./bootstrap');


const routes = new VueRouter(
    {
        mode:'history',
        routes:[
            {
                path:'/',
                component:ExampleComponent
            }
        ]
    }
);
const app = new Vue({
    el: '#app',
    routes
});

/resources/js/bootstrap.js content:

window._ = require('lodash');

try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

app.blade.php content:

<!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 href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div id="app">
    @yield('content')
</div>
</body>
</html>

welcome.blade.php content:

@extends('layouts.app')

@section('content')
    <div class="container">
        <router-view></router-view>
    </div>
@endsection

laravel web.php content:

Route::get('/{any}', function () {
    return view('welcome');
})->where('any','.*');

simple piece of laravel composer.json:

"require": {
    "php": "^7.3|^8.0",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.12",
    "laravel/tinker": "^2.5",
    "laravel/ui": "^3.2"
},

webpack.mix.js content:

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

mix.js('resources/js/app.js', 'public/js')
    .vue()
    .sass('resources/sass/app.scss', 'public/css');

and then:

php artisan serve && npm run watch

i have empty page after navigating to / in browser http://127.0.0.1:8000

how can i resolve this issue to use vuejs?

2 Answers 2

1

Concerning Vue router, what I usually have is a separated router.js file because it can be lengthy:

import Vue from 'vue'
import Router from 'vue-router'
...
Vue.use(Router)
const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/users',
      name: 'users',
      component: () => import('./views/users/_users.vue'),
    }
    ...
 ]

 export default router

And in main.js I have:

import router from './router' // path to router.js file
...
Vue.router = router
...

export default new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
Sign up to request clarification or add additional context in comments.

4 Comments

what are store and render in your code?
store is for Vuex I forgot to remove it I apologize, about render forum.vuejs.org/t/what-does-render-h-h-app-mean/53313 expand the answer to see it fully
thanks so much, i have laravel with vuejs now
I'm glad it helped.. good luck at that and enjoy coding
1

These constraints are appearently superfluous: ->where('any','.*').

And the route might not match - or .htaccess might be absent;
eg. to capture regex (.*) into variable $page for any method:

Route::any('(.*)', function($page) {
    // dd($page);
    return view('welcome');
});

Not sure, but you probably also need to Vue.use(VueRouter); the docs also initialize differently:

const app = new Vue({routes}).$mount('#app')

I mean, there is no debug information and there are even two ways how to install VueRouter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.