简体   繁体   English

重新加载页面时找不到vue js:路由

[英]vue js:routes not found when page reloads

Here is simple vue-routing 这是简单的vue路由

const Foo = { template: '<div>foo</div>' }
  const Bar = { template: '<div>bar</div>' }


  const routes = [
    { path: '/foo', component: Foo },
    { path: '/bar', component: Bar }
  ];
  const router = new VueRouter({
    routes:routes,
    mode: 'history'
  });

  var app = new Vue({
  router,
  data: {
    message: 'Hello Vue!'
  },
  mounted:function(){

  }
}).$mount('#app');

In HTML HTML中

    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
    <router-view></router-view>

The links loaded properly (ie : /foo and /bar ) when i click on it but if i refresh any vue routed url (ie /foo or /bar ) url i will get error message 当我单击链接时,链接已正确加载(即: /foo/bar ),但如果刷新任何Vue路由的网址(即/foo/bar ),我将收到错误消息

cannot get the page 无法获取页面

I am serving it with simple express server 我通过简单的快递服务器提供服务

app.get('/', (req, res) => res.sendFile('index.html',{ root : __dirname}))

You have to redirect all the routes to main file index.html . 您必须将所有路由重定向到主文件index.html

app.get('/', (req, res) => res.sendFile('index.html',{ root : __dirname}))
app.get('*', (req, res) => res.sendFile('index.html',{ root : __dirname}))

You have to redirect all the routes to main file index.html , but after of your route main, so: 您必须将所有路由重定向到主文件index.html ,但是在路由main之后,因此:

//route main
app.use('/api/example', require('./routes/example')); 
//routes auxiliary
app.get('/', (req, res) => res.sendFile('index.html', { root : __dirname + '/public'}))
app.get('*', (req, res) => res.sendFile('index.html', { root : __dirname + '/public'}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM