3

My project uses Vue2 and Vue Router. I want to be able to load the component Stats by typing in the URL http://example.com/stats.

  1. URL http://example.com/stats does not work, I'm redirected to / and it loads the App component
  2. <router-link :to="/stats">Go to stats</router-link> works perfectly

I would like to know whether it is a Vue Router issue or a server configuration issue. I'd like to mention the fact that I experience this issue both in my localhost and my server using nGinx.

How could I fix this?

app.js:

const routes = [
  { path: '/', component: App },
  { path: '/stats', component: Stats },
  { path: "*", component: PageNotFound },
  { path: '/admin', meta: { requiresAdmin: true }, component: Admin},
  { path: "/not-authorized", component: NotAuthorized },
];

Vue.use(VueRouter);
    
const router = new VueRouter({
  mode: 'history',
  routes,
})

const app = new Vue({
  el: '#app',
  router,
  data () {
    return {}
  },
});

router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAdmin)) {
        if (!Store.state.isAdmin) {
            axios.post('/isAdmin').then((response) => {
                if(response.data.isAdmin){
                    next();
                }
                else {
                    next({
                        path: '/not-authorized',
                    })
                }
            });
        }
        else {
            next();
        }
    }
        else {
            next();
        }
    }
    else {
        next(); // make sure to always call next()!
    }
});
12
  • Do you also have new Vue({ el: '#app', router }) somewhere? Commented Apr 7, 2017 at 16:13
  • Of course, sorry I just reduce the app.js to what it seemed to me important. I just updated it ! Commented Apr 7, 2017 at 16:35
  • and you're calling Vue.use(VueRouter) at some point? Commented Apr 7, 2017 at 16:49
  • Yes I do. When I'm clicking on a <router-link> in my code I'm well redirected to my component. I updated the app.js file Commented Apr 7, 2017 at 17:08
  • What's the generated URI when you click on the link? Did you disable the # when instantiating the router? Commented Apr 7, 2017 at 17:20

3 Answers 3

1

Have you configured your web-server to return the same page regardless of the URL? That way your app can load, and the URL is preserved so routing can take over and select the right component once it's loaded.

This answer https://stackoverflow.com/a/7027686/7816087 suggests the following config for nginx:

location / {
    try_files $uri /base.html;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does not work, the URL is changed to / and then the App component is loaded
1

Go to your Laravel routes and put this:

Route::any('{all}', function () {
    return view('index'); // if your main view is "index"
})

This will ensure that deep linking is working properly on all request handled by Laravel.

2 Comments

Where does OP mention Laravel?
@Phil I looked his previous posts and array of technologies. The accepted answer is also for Laravel.
0

Here is what I did

Route::get('/stats', 'HomeController@home');

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.