1

Not a duplicate: already check this question and this one; didn't get any clue. Some of similar ones are related to Laravel.

Can't figure out what was changed in my project, making [Vue warn]: Cannot find element: #app appear.

The structure:

index.html: hosts the <div id="app">:

[HEADER]

<div id="app" v-cloak></div>

[FOOTER]

main.js:

import Vue from 'vue';
import Users from './Users.vue';
import App from './App.vue';             // (1)
import Home from './Home.vue';           // (2)
import VueRouter from 'vue-router';      // (3)

Vue.use(VueRouter);

const routes = [
  {path: '/users', component: Users},
  {path: '/', component: Home}
];

const router = new VueRouter({
  routes
});

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

EDIT: when changed the last block to:

$(document).ready(function () {
  new Vue({
    el: '#app',
    router,
    render: h => h(App)
  })
})

it didn't help either.

App.vue:

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

<script>
export default {
  data () {
    return {
      message: "Test message"
    }
  }
}
</script>

Home.vue:

<template>
    [contents]
    [router-links]
</template>

<script>
    [data]
    [methods]
</script>

Whenever I get to the index page, the Vue warn gets displayed. One of attached questions I checked suggested that the DOM element is not ready when scripts are running. Already tried to switch order of imports in main.js (App, Home and VueRouter, as marked in comments), but with no success.

Any idea where to look for solution?

AbreQueVoy
  • 1,748
  • 8
  • 31
  • 52

1 Answers1

2

Okay, found the bug:

index.html:

(...)
      </footer>
   <script src="../dist/build.js"></script>
</body>

The script over there is located in a build directory. However, webpack generates its own script file on the fly and runs the application from it. The script mentioned above in the code was simply overwriting values correctly loaded before. Deleting the ../dist/ directory and commenting the script line out brought everything back to normal.

AbreQueVoy
  • 1,748
  • 8
  • 31
  • 52