2

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

2
  • 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> Commented Jul 21, 2019 at 0:22
  • 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? Commented Jul 21, 2019 at 0:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Man, I can't believe I overlooked something that silly.. But yeah, that seemed to fix it. As for the window method would I just create a module with my Axios configuration and import it where needed? Because yeah I noticed whilst trying to fix it that window.Vue was pretty much redundant.
Just remove the code which assigns to window; axios will still be configured if you import it elsewhere.

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.