[Vue warn]: Cannot find element — empty container is rendered

心不动则不痛 提交于 2019-12-12 04:36:52

问题


I am trying to learn Vue 2 framework and I am stuck with this error. I have the following scripts responsible for the functionality of my application:

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import App from './App.vue'
import Posts from './components/Posts.vue'
import Routes from './routes/routes'

Vue.config.productionTip = false

Vue.use(VueRouter)
Vue.use(VueResource)

const router = new VueRouter({
  routes: Routes
})

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

App.vue

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

</template>

<script>
  import Posts from './components/Posts.vue'

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

<template>
    <div id='root'>
        <h1>Test</h1>   
        <input type="text" v-model="message">
    </div>
</template>

Posts.vue

<script>
    import Vue from 'vue'

    window.addEventListener('load', function () {
        new Vue({
            el: '#root',
            data: {
                message: 'Hello World!'
            }
        })
    })
</script>

Those scripts are included at the end of the page. Important to mention that the div with id value of 'app' is empty. I am also new to webpack, so I would assume, that there's a problem with my imports and exports, but unfortunately I could not to resolve it myself. Index page simply contains this:

<div id="app"></div>

UPD (router.js)

import Posts from '../components/Posts.vue'

const routes = [
   { path: '/', component: Posts }
]

export default routes 

回答1:


In App.vue

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

The <router-view> component will show the components as per the current route.

import Posts from '../components/Posts.vue'

const routes = [
 { path: '/', component: Posts }
]

export default routes 

Here it is clear that <router-view> would show the Posts component.

Posts.vue

<template>
  <div id='root'>
    <h1>Test</h1>   
    <input type="text" v-model="message">
  </div>
</template>

<script>
import Vue from 'vue'

window.addEventListener('load', function () {
    new Vue({
        el: '#root',
        data: {
            message: 'Hello World!'
        }
    })
})
</script>

Flashback to the router, there is a line which says:

import Posts from '../components/Posts.vue'

this is the import default syntax but what has been exported by Posts.vue ? Hence, there is nothing imported to the router. Hence, nothing rendered inside the #app.

Also, posts doesn't have to be a new Vue(...) instance.

<template>
  <div id='root'>
    <h1>Test</h1>   
    <input type="text" v-model="message">
  </div>
</template>

<script>
import Vue from 'vue'

export default {
  data: {
    message: 'Hello World!'
  }
}
</script>

Will do just fine. With that, there is a default export in place for the router to use.



来源:https://stackoverflow.com/questions/43053309/vue-warn-cannot-find-element-empty-container-is-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!