最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Second level vue route throws error when accessed directly - Stack Overflow

programmeradmin3浏览0评论

I am using vue-router in my vue application. I have the following routes in my application.

const routes = [
    {name: "home", path: "/", component: Home},
    {name: "user", path: "/user", component: User},
    {name: "edit-user", path: "/user/edit", component: EditUser}
];

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

Here both the routes are working perfectly when accessed using router-link as given below.

<router-link to="/user">Go to user</router-link>
<router-link to="/user/edit">Edit user</router-link>

If I am accessing the routes directly by page refresh the route "/user" works perfectly. But the route "/user/edit" shows a black page with just the following error in the console (without any other error details).

Uncaught SyntaxError: Unexpected token <

Can anyone help me out to solve this?

I am using vue-router in my vue application. I have the following routes in my application.

const routes = [
    {name: "home", path: "/", component: Home},
    {name: "user", path: "/user", component: User},
    {name: "edit-user", path: "/user/edit", component: EditUser}
];

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

Here both the routes are working perfectly when accessed using router-link as given below.

<router-link to="/user">Go to user</router-link>
<router-link to="/user/edit">Edit user</router-link>

If I am accessing the routes directly by page refresh the route "/user" works perfectly. But the route "/user/edit" shows a black page with just the following error in the console (without any other error details).

Uncaught SyntaxError: Unexpected token <

Can anyone help me out to solve this?

Share Improve this question edited Mar 14, 2018 at 12:08 Arun D asked Mar 14, 2018 at 11:40 Arun DArun D 2,3875 gold badges30 silver badges41 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

I was facing the same issue I resolved this by adding

publicPath: '/'

in my Webpack config file suggested on https://github.com/webpack/webpack-dev-middleware/issues/205#issuecomment-315847782

Thanks to Mark M. Muskardin

It was caused due to a very small reason. In my main HTML page, the js file was linked as follows:

<script type="text/javascript" src="js/app.js"></script>

The issue was fixed by changing the src value from "js/app.js" to "/js/app.js" as follows:

<script type="text/javascript" src="/js/app.js"></script>
发布评论

评论列表(0)

  1. 暂无评论