【问题标题】:Vue-router only showing the base routeVue-router 只显示基本路由
【发布时间】:2019-10-20 23:44:09
【问题描述】:

我正在尝试使用 vue-router。但是,我遇到了一个问题,在查看了许多文章和文档后我找不到解决方案。

我已经阅读了几篇关于如何使用 vue-router 的文章,但它们都不起作用。

这是我的代码。 我做了三个组件HomeLoginHelloWorld

我已经做了三条路线。

这里是代码

main.js:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
// import Home from './components/Home.vue';
import HelloWorld from './components/HelloWorld.vue';
import Login from './components/Login.vue';


Vue.use(VueRouter)

Vue.config.productionTip = false


const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes: [
    { path: '/hello', component: HelloWorld },
    { path: '/login', component: Login },
    { path: '/', component: App }
  ]
});


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

App.vue:

<template>
  <div id="app">
    <HelloWorld/>
    <Login />
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import Login from './components/Login.vue'

export default {
  name: 'app',
  components: {
    HelloWorld,
    Login
  }
}
</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>

登录.vue:

<template>
  <div>
    <h1>Login </h1>    
  </div>
</template>

<script>
export default {
  name: 'Login',

}
</script>

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

HelloWorld.vue:

<template>
  <div>
    <h1>Hello World </h1>    
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',

}
</script>

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

package.json

  "name": "test-vue-router",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^2.6.5",
    "vue": "^2.6.10",
    "vue-router": "^3.0.6"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.8.0",
    "@vue/cli-plugin-eslint": "^3.8.0",
    "@vue/cli-service": "^3.8.0",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "vue-template-compiler": "^2.6.10"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ]
}

两条路线都不起作用。 每次,我通过更改 URL 按回车键。基本路线(即/)总是出现。

它显示 App.vue 组件。

谢谢!

【问题讨论】:

  • 尝试将您当前的第三条路线 (/) 作为第一条路线。

标签: javascript vue.js vue-router


【解决方案1】:

您似乎只是缺少 App.vue 上的 &lt;router-view/&gt; 标签。 &lt;router-view/&gt; 将根据 url 呈现您的组件,因此您无需自己导入组件。

试试这个:

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

【讨论】:

    【解决方案2】:

    app.vue 中的模板不正确, 试试这个:

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

    注意&lt;router-view/&gt; 标签是你的页面/组件将被渲染的地方。

    此外,您的 routes 数组上的基本路由 / 不正确,App 组件是根组件,它将持有其他页面/组件作为其子组件,因此 App 组件不能是路由之一。

    【讨论】:

    • 真的,你救了我。否则,我会被困在那里一辈子。谢谢 :) @大侯
    • 顺便说一句。关于我应该为路由设置哪个组件的任何想法/
    • @abhigyannayak 没问题 :),所以 / 路线就像 index.htmlindex.php 如果你知道它们是什么,基本上它是你想看到的主要组件/页面/在您的应用加载时渲染,无需任何额外路径,因此您可以在那里选择您想要的内容。
    • 它的index.html。所以必须放在那里吗?尽管App.vue
    • @abhigyannayak 不,这只是使用静态网站时的一个例子。但是您可以创建一个新组件(例如:index.vue)并将其设置为 / 路由,这将是您的登录页面,例如,将域名 example.com 作为您的 Web 应用程序域,所以无论您想要什么在访问者访问此网址时向他们显示 example.com/ 将其放入 index.vue 文件中。
    猜你喜欢
    • 2021-05-21
    • 2019-08-02
    • 1970-01-01
    • 2021-12-29
    • 2021-01-14
    • 2017-11-26
    • 2021-06-29
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多