2
votes

I am trying to make a vue SPA using vuex, vue-router & laravel for backend. I was separating our data on our app.js to try to reduce clutter and keep our code neat. When everything on one page it works as intended, loading the routes in the router. But when we separate the code to make it more modular into: app.js, boostrap.js, routes.js, and store.js

The components aren't loading in our router-view and we are able to see our RouterLink

app.js


    // Require the bootstrapper
    require('./bootstrap');

    // Grab imports
    import Store from './store';
    import Router from './routes';

    // Views
    import App from './views/App';

    // Create the application
    const app = new Vue({
      el: '#heroic',
      components: { App },
      store: Store,
      router: Router
    });

boostrap.js


    // Imports
    import Vue from 'vue';
    import Axios from 'axios';
    import Swal from 'sweetalert2';

    // Add to window
    window.Vue = Vue;
    window.Axios = Axios;

    // Add Axios headers
    window.Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    window.Axios.defaults.headers.common['Authorization'] = 'Bearer ' + 'token';
    window.Axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

routes.js


    // Imports
    import Vue from 'vue';
    import VueRouter from 'vue-router';
    import Store from './store';

    // Set to use
    Vue.use(VueRouter);

    // Views
    import Hello from './views/Hello';
    import Home from './views/Home/';
    import UserIndex from './views/UserIndex';

    // Create our routes
    const routes = [
      {
        path: '/',
        name: 'home',
        component: Home,
      },
      {
        path: '/hello',
        name: 'hello',
        component: Hello,
      },
      {
        path: '/users',
        name: 'users.index',
        component: UserIndex,
      }
    ];

    // Create the router
    const router = new VueRouter({
      mode: 'history',
      routes: routes,
      scrollBehavior (to, from, saved) {
        if (saved) {
          return saved;
        }
        return { x: 0, y: 0};
      }
    });

    // Before every request
    router.beforeEach((to, from, next) => {

    });

    // After every request
    router.afterEach((to, from, next) => {

    });

    // Export
    export default router;

hello.vue


    <template>
      <div class="row row-cards row-deck">
        <div class="col-lg-4 col-md-6">
          <p>Hello World!</p>
        </div>
      </div>
    </template>

store.js


    // Imports
    import Vue from 'vue';
    import Vuex from 'vuex';
    import PersistedState from 'vuex-persistedstate';
    import Cookie from 'js-cookie';

    // Set use
    Vue.use(Vuex);

    // Create our store
    const store = new Vuex.Store({
      state: {
        auth: [{
          id: 1,
          username: '',
          motto: '',
          rank: 1,
          permissions: [],
          token: ''
        }],
        users: [],
      },
      mutations:{

      },
      actions: {

      },
      getters: {

      }
    });

    // Export
    export default store;

The expected result is that when I visit the "/hello" route it would show the information that says "Hello world!" that is within the Vue file specified as the component in the routes section of the router. Instead using my Vue DevTools I get the following with no Hello world on the page.

https://i.pathetic.site/chrome_99Mbxf7f0c.png

1
you have an error in console. what is it saying? btw, I'm not sure that is possible to decouple 'View' parts of components and then import it, every component should have template + script, return the template into app.vue with <router-view></router-view>DedaDev
The error in the console is regarding a JS error with the Bootstrap Admin template that I bought off of ThemeForest, so I don't think that is necessarily related but it's this; i.pathetic.site/QfG5FdqhEM.png. With that said I'm not sure what you mean by return the template into app.vue?Jake Adams

1 Answers

1
votes

My guess is the router is stuck waiting for the beforeEach (and also possibly afterEach) hook to be resolved. You need to call next().

Also unrelated, but if you’re using modules then you shouldn’t need to assign stuff on window.