【问题标题】:Vuejs router cant reach any componentsVuejs路由器无法访问任何组件
【发布时间】:2018-07-07 17:16:27
【问题描述】:

我可能使用了一些过时的指南,这可能是它的结果,对 Vuejs 来说是新的。
我的App.vue

<template>
  <div id="app">
    <img src="../assets/logo.png">
      <router-view/>
    </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
  #app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
  }
</style>

我的 ma​​in.js

import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})

路由器/index.js

import Vue from 'vue'
import Router from 'vue-router'
import { routes } from '../app'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: routes
})

然后是 app/routes.js

import accounts from './accounts'
import budgets from './budgets'
import transactions from './transactions'

export default [...accounts, ...budgets, ...transactions]

最后,app/accounts/routes.js

import * as components from './components'

export default [
  {
    path: '/',
    component: components.AccountsListView,
    name: 'accountsListView' 
  },
  {
    path: '/accounts/create',
    component: components.CreateEditAccounts,
    name: 'createAccounts'
  },
  {
    path: '/accounts/edit',
    component: componenets.CreateEditAccounts,
    name: 'editAccounts'
  }
]

由于某种原因,我无法访问我的任何组件,例如,如果我转到 localhost:8080/accounts/create,那么我最终会进入带有更改的 url 的索引页面,而不是访问相应的组件。我所做的事情有什么明显的问题吗?

【问题讨论】:

  • 浏览器为开发人员提供了这个工具……称为开发人员工具控制台……你检查过你的错误吗?没关系,这是服务器端的问题,对吧?
  • 控制台没有任何错误

标签: javascript vue.js vue-router


【解决方案1】:

对于这个出口

export default [
  {
    path: '/',
    component: components.AccountsListView,
    name: 'accountsListView' 
  },
  {
    path: '/accounts/create',
    component: components.CreateEditAccounts,
    name: 'createAccounts'
  },
  {
    path: '/accounts/edit',
    component: componenets.CreateEditAccounts,
    name: 'editAccounts'
  }
]

由于路由数组是默认导出,因此您需要使用此导入。
对象样式导入适用于有多个导出的情况。

import routes from '../app'

参考:MDN web docs - export

【讨论】:

  • 我稍微更新了这个问题,似乎 SO 上的代码编辑器已经吃掉了 routes.js 的第一行,即import * as components from './components'。我应该把你的导入放在 app/accounts/routes.js 的顶部吗?
  • 不,这是修改后的 app/routes.js 导入。
  • 哦等等 - 混乱 - router/index.jsapp/routes.js 似乎是一样的。在任何情况下,您的路由数组的导入都不应该有大括号。
  • 修正了问题
  • 好的,很酷。只需更改 router/index.js 中的导入,因为 app/routes.js 的默认导出仍然是一个数组。 (顺便说一句,也许应该是import routes from '../app/routes'
【解决方案2】:

ma​​in.js:

import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
})

也许您正在使用仅运行时构建的 Vue,而模板编译器不可用。

编辑#1

App.vue

<template>
  <div class="App">
      <router-view />
  </div>
</template>

那么你可能没有在你的App 组件中包含router-view

【讨论】:

  • 还是同样的问题。我想我使用的是“正确”版本,因为我可以看到我的 App.vue
  • 那我怀疑你的 App 组件中没有包含 router-view。
  • 很遗憾,我没有提到它,但它就在那里
  • 好的。然后它似乎是@Richard Matsen 指出的导入错误
猜你喜欢
  • 2015-06-29
  • 2021-11-27
  • 2023-03-27
  • 2018-07-04
  • 2017-10-02
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
相关资源
最近更新 更多