javascript - Vue 路由器不加载组件

标签 javascript vuejs2 vue-router

所以我一直在研究 vue 并遇到一个我似乎无法找到解决方案的问题。我正在使用 Vue 和 Vue-router。我从提供初始样板的基本 vue + webpack 模板开始。

我已经成功地将额外的路线添加到预定义的 route ,它按预期工作(游戏、锦标赛、统计和用户路线工作得很好)。但是现在我无法获得额外的工作路线。 “游戏”路线不起作用,我还尝试添加其他似乎也不起作用的路线。

所以这是我当前的路由器文件 (index.js):

import Vue from 'vue'
import Router from 'vue-router'
const Gaming = () => import('@/components/gaming.vue');
const Home = () => import('@/components/home.vue');
const Game = () => import('@/components/game.vue');
const Tournament = () => import('@/components/tournament.vue');
const Users = () => import('@/components/users.vue');
const Stats = () => import('@/components/stats.vue');


const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/games',
      name: 'Game',
      component: Game,
    },
    {
      path: '/wtf',
      name: 'Gaming',
      components: Gaming,
    },
    {
      path: '/tournaments',
      name: 'Tournament',
      component: Tournament
    },
    {
      path: '/users',
      name: 'Users',
      component: Users
    },
    {
      path: '/stats',
      name: 'Stats',
      component: Stats
    }
  ]
});

export default router;

Vue.use(Router);

除“游戏”路线外,我的所有路线都按预期工作。 “游戏”组件如下所示:

<template>
  <div>
    <h1>WTF?!?!?!?!?=!?!</h1>
  </div>
</template>

<script>
  export default {
    name: 'Gaming',
    components: {},
    data() {
      return {}
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
</style>

我几乎尝试复制/粘贴一个工作组件,并且只更改名称和模板。但它似乎有问题。最初我完成了路由组件导入正常的“样板”方式

import Stats from '@/components/Stats'

这几乎有相同的结果,除了这会在尝试导航到“游戏”路线时导致异常。

Cannot read property '$createElement' of undefined
    at render (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-c9036282","hasScoped":false,"transformToRequire":{"video":["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/gaming.vue (app.js:4240), <anonymous>:3:16)

所有其他路线都有效。我还尝试重新创建所有文件并重新执行似乎也不起作用的特定路径。所以我不知道我能做些什么来解决这个问题?

我在这里尝试检查路线,正如您所见,该组件缺少“一切” Inspecting the route

还尝试查看 chrome 的 vue 插件,其中组件未加载到 View 中

Vue Chrome Extension

如果有人想调整它,请将项目上传到 gdrive Google Drive Link

最佳答案

发现问题:

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/games',
      name: 'Game',
      component: Game,
    },
    {
      path: '/wtf',
      name: 'Gaming',
      components <----------: Gaming,
      // Should be component not componentS
    },
    {
      path: '/tournaments',
      name: 'Tournament',
      component: Tournament
    },
    ...

此外,您应该使用标准的导入方法。如果没有那个错误,我永远不会发现问题。

关于javascript - Vue 路由器不加载组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122512/

相关文章:

php - 如何在 Vue Router 中获得 404 响应

javascript - 将嵌套对象写入 Apollo 客户端缓存

javascript - 将变量传递给 setState

javascript - 在 <td> 中查找文本,然后有条件地更改另一个 <td> 中的文本

javascript - vuex- 状态作为函数或对象文字返回

javascript - 使用 vue.js 更改组件中的变量

javascript - 如何从其他组件的事件监听器($on)调用此组件方法?

javascript - 您能为我解释一下 Jquery 中的这种 JavaScript 语法吗?

javascript - 如何从服务器拉取 Vue Router 页面?

javascript - Vue.js - 更新路由器 View