首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue-router仅显示基本路由

Vue-router仅显示基本路由
EN

Stack Overflow用户
提问于 2019-06-05 20:54:04
回答 2查看 3.9K关注 0票数 3

我正尝试在vue-router上工作。但是,我被一个问题困住了,在看了很多文章和文档后,我找不到解决方案。

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

这是我的代码。我已经为Login HelloWorld创建了三个组件

我做了三条路线。

以下是代码

main.js:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

Login.vue:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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

代码语言:javascript
复制
  "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来按enter键。基本路由(即/)总是会出现。

它显示了App.vue组件。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-05 21:13:55

您在app.vue中的模板不正确,请尝试以下操作:

代码语言:javascript
复制
<template>
  <div id="app">
    <router-view/>
  </div>
</template>

请注意,<router-view/>标记是呈现页面/组件的位置。

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

票数 4
EN

Stack Overflow用户

发布于 2019-06-05 21:14:49

看起来你只是错过了App.vue上的<router-view/>标签。<router-view/>会根据url来渲染你的组件,所以你不需要自己导入组件。

试试这个:

代码语言:javascript
复制
<template>
  <div id="app">
    <router-view/>
  </div>
</template>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56460910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档