Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vue-router <router-view>中内容没有渲染出来? vue-router的原理是什么?

入门vue-router的时候遇到一个问题。
<router-view> tag 中内容没有渲染出来,查阅了一下vue-router文档,没有搞懂是哪里出的问题.

非常好奇,vue-router的原理是什么?

搜索了一下,这儿有一个相同的问题,但是还没有获得很好的回答。

现象,已经跳转到page的url,但是没有渲染出期望的page内容,期望的页面应该是会出现“This is Page~”一行字:
现象

文档结构:
文档结构

相关代码:
main.js

import Vue from 'vue'
import VueRouter from 'vue-router'

import App from './App.vue'

Vue.use(VueRouter)

import routers from './router'
const router = new VueRouter({
  mode: 'history',
  routers
})

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

App.vue

<template>
  <div id="app">
    <ul>
      <li><router-link to="/home">Home</router-link></li>
      <li><router-link to="/page">Page</router-link></li>
    </ul>

    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data () {
    return {
    }
  }
}
</script>

router.js

import Home from './component/home/index.vue'
import Page from './component/page/index.vue'

const routers = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/page',
    name: 'page',
    component: Page
  },
]

Home/index.vue (Page的代码类似)

<template>
  <h1>This is Home~</h1>
</template>

<script>
export default {
  data () {
    return {
      
    }
  }
}
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

那个问题下已经有答案了

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

是 routes 不是 routers

关于原理可以参考 https://github.com/DDFE/DDFE-...https://github.com/DDFE/DDFE-...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...