【问题标题】:Vue-router doesn't show a component when visiting the correspondent pathVue-router 访问对应路径时不显示组件
【发布时间】:2020-12-22 19:04:01
【问题描述】:

路由器工作不正常。它看起来像工作

应用运行于:

  • 本地:http://localhost:8080/

但是路由器好像看不到组件((

我的路由器

import Vue from 'vue'
import VueRouter from 'vue-router'
import Web from '@/views/Web.vue'


Vue.use(VueRouter)

  const routes = [
  {
    path: '/web',
    name: 'Web',
    component: Web
  },

  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: function () {
      return import(/* webpackChunkName: "about" */ '../views/About.vue')
    }

  }
]

const router = new VueRouter({
  routes
})

export default router

我的组件:

<template>
    <h1>Hello</h1>
</template>

<script>

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

我的错误:

 App running at:
  - Local:   http://localhost:8080/

但是什么也没发生(

它会是什么?

我的 package.json 中可能需要一些依赖项吗?

{
  "name": "osprey",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve ",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "autoprefixer": "^9.8.6",
    "live-server": "^1.2.1",
    "vue": "^2.6.11",
    "vue-router": "^3.2.0",
    "vuex": "^3.4.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-router": "~4.4.0",
    "@vue/cli-plugin-vuex": "~4.4.0",
    "@vue/cli-service": "~4.4.0",
    "node-sass": "^4.9.0",
    "sass-loader": "^7.0.1",
    "vue-template-compiler": "^2.5.17"
  }
}

或者我的 vue.config.json 中的什么?

module.exports = {
    css: {
      loaderOptions: {
        sass: {
          data: `@import "@/styles/_variables.scss";`
        }
      }
    }
  };

【问题讨论】:

  • 请提供一个有意义的标题
  • 您必须在此问题中包含更多详细信息。比如,有什么问题?你期望什么,会发生什么……?
  • 我的组件 Web 没有在我的路径中显示“Hello”:'/web',..那里显示了我的 'App'
  • @JustinMoreyl 你试过我的答案了吗?

标签: vue.js vuejs2 vue-component vuex vue-router


【解决方案1】:

将模式历史添加到您的路由器配置中,如下所示:

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

为了访问您的路径,例如/web,但在您的情况下,您必须使用#/web,因为默认模式是哈希

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2019-08-02
    • 2021-05-07
    • 2021-11-27
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多