【问题标题】:How can I redirect using vue-router?如何使用 vue-router 进行重定向?
【发布时间】:2020-06-03 07:17:58
【问题描述】:

我曾尝试通过 url 访问其他页面,但这些页面永远不会加载

这是我的 main.js 文件

import Vue from 'vue'
import App from './App.vue'
import vuetify from './plugins/vuetify';
import VueRouter from 'vue-router'

Vue.config.productionTip = false

Vue.use(VueRouter)

import Index from './views/Index';
const About = { template: '<p>about page</p>' }

const routes = [
  { path: '/index', name: 'index', component: Index },
  { path: '/about', name: 'about', component: About }
]

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

new Vue({
  router: router,
  vuetify,
  render: h => h(App)
}).$mount('#app')

有人可以帮助我使用 vue-router 吗?我是这个框架的新手

【问题讨论】:

标签: vue.js vue-router


【解决方案1】:

如果你这样访问它们是否有效:

http://localhost:8080/#/about

如果是,您的vue-router 正在默认的hash-mode 下工作。要摆脱哈希并获得“正常” URL,您需要将其设置为 history mode

编辑:

我看到你已经在使用历史模式了。您是否使用 Vue CLI 进行本地开发?这通常应该是work out of the box。如果没有,您需要在其他 Web 服务器上设置一些重定向规则。请在此处查看示例:Example Server Configurations

编辑 2: 你能展示你的App 组件吗? 我试图在沙盒中重现您的问题,但它有效:https://codesandbox.io/s/confident-voice-zyg07

这里的App 组件看起来像这样,包括路由器视图:

<template>
  <div id="app">
    <router-view id="page"/>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {}
};
</script>

【讨论】:

  • 请看我的编辑。好像您已经在使用历史模式。如果您不使用 Vue CLI,也许您缺少本地 Web 服务器的重定向规则?
猜你喜欢
  • 2020-10-08
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 2019-09-27
  • 2019-11-10
  • 2014-10-12
  • 2020-07-17
  • 2020-01-11
相关资源
最近更新 更多