【问题标题】:Rendering a component with router in Vue.js在 Vue.js 中使用路由器渲染组件
【发布时间】:2021-01-17 06:35:53
【问题描述】:

我使用 Vue 的第一天。我初始化了我的应用程序。我想添加一个登录组件,当我在 main.js 中设置路由时,没有页面工作。

import Vue from 'vue'
import App from './App.vue'

import VueRouter from 'vue-router'

import Login from './components/Login.vue'

const routes = [
  { path: '/', component: App },
  { path: '/login', component: Login }
]

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

new Vue({
  router
}).$mount("#app")

仅当我向 Vue 对象添加渲染选项并渲染 App 组件时才有效。

new Vue({
  router,
  render: h => h(App) // or Login
}).$mount("#app")

如何确保渲染路由组件?

【问题讨论】:

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


    【解决方案1】:

    您应该在 App 组件中添加 router-view 组件,例如:

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

    【讨论】:

    • 非常感谢。现在,我看不到Login的内容,我看到的只是App.vue的模板。
    • 您可以访问链接youbaseUrl/login 类似http://127.0.0.1:8080/login 请检查此full example
    猜你喜欢
    • 2021-01-18
    • 2016-08-10
    • 2022-01-18
    • 2021-04-24
    • 2019-04-25
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多