【问题标题】:Vue - Unable to render componentVue - 无法渲染组件
【发布时间】:2019-04-19 01:52:54
【问题描述】:

某些组件无法注入到 App.vue 模板中。特别是,Login.vue 是导致问题的原因。如果我在服务器运行时从头开始创建它 (npm run dev) 它将工作,但是一旦我重新启动它并导航到登录组件,它将无法加载。

堆栈跟踪

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <Login>
       <App> at src/App.vue
         <Root>

src/components/Login.vue

<template>
  <div id="login">
    <ul>
      <li><input type="text" v-model="username" placeholder="Username"></li>
      <li><input type="password" v-model="password" placeholder="Password"></li>
      <li><button @click="login(username, password)">Login</button></li>
    </ul>
  </div>
</template>

<script>
</script>

<style scoped>

#login{
  padding-top: 100px;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

</style>

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import RideSharing from '@/components/RideSharing'
import Login from '@/components/Login'
import About from '@/components/About'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Hello',
      component: Hello
    },
    {
      path: '/app',
      name: 'RideSharing',
      component: RideSharing
    },
    {
      path: '/app/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/app/about',
      name: 'About',
      component: About
    }
  ]
})

src/App.vue

<template>
  <div id="app" :style="{padding: $route.path === '/' ? '172px' : '10px'}">
    <div>
      <img src="./assets/logo4.png" align="center">
    </div>
    <router-view></router-view>
  </div>
</template>

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

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  background-color: hsla(269, 96%, 50%, 0.73);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 0px;
  height: inherit;
}

body{
  height: inherit;
}

html{
  height: 100%;
}
</style>

src/main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import BootstrapVue from "bootstrap-vue"
import App from './App'
import router from './router'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(BootstrapVue)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

【问题讨论】:

  • 可以分享一下 login.js 文件吗?
  • App中不应该包含Login组件吗?
  • 那你为什么有:
  • @Efrat 抱歉,我忘了省略那部分。 login.js 文件是空的,不是问题的根源。
  • @Jayem163 脚本指针不应该存在。编辑代码

标签: javascript html vue.js


【解决方案1】:

如果您使用的是完整版本的 Vue,那么您可以尝试使用 .$mount('#app') 挂载 DOM 模板

因此,在您的 main.js 中,您的应用脚本将如下所示:

new Vue({
  router,
  template: '<App/>',
  components: { App },
}).$mount('#app')

【讨论】:

    【解决方案2】:

    解决方案:

    导入中缺少 .vue 扩展。

    src/router/index.js

    import Vue from 'vue'
    import Router from 'vue-router'
    import Hello from '@/components/Hello.vue'
    import RideSharing from '@/components/RideSharing.vue'
    import Login from '@/components/Login.vue'
    import About from '@/components/About.vue'
    

    【讨论】:

      猜你喜欢
      • 2019-02-19
      • 1970-01-01
      • 2020-01-19
      • 2017-10-24
      • 2018-08-05
      • 1970-01-01
      • 2020-10-12
      • 2020-03-25
      • 2017-11-09
      相关资源
      最近更新 更多