I am attempting to use Vue.js to recreate a frontend app I previously made using Scala Play (I'd like to learn more about component-based web design).
I had everything loading fine for the first page I made, but I wanted to implement routing so I could have multiple pages (rather than just having one big single-page application). I installed vue-router using npm install vue-router --save, and am trying to follow the following tutorial (using the components I've already made though): https://scotch.io/tutorials/how-to-build-a-simple-single-page-application-using-vue-2-part-1.
As far as I can tell, everything should be loading smoothly, but nothing loads and I get the following console errors:
[Vue warn]: Cannot find element: #app
[Vue warn]: Cannot find element: #app
[Vue warn]: Error in render: "TypeError: Cannot read property 'matched' of undefined"
found in
---> <App> at src/App.vue
<Root>
TypeError: Cannot read property 'matched' of undefined
at render (vue-router.esm.js?8c4f:76)
at createFunctionalComponent (vue.runtime.esm.js?2b0e:3033)
.... (etc)
I've not pasted the full stack traces but can do if needed.
My code is below:
main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Home from './components/Home.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: Home }
]
const router = new VueRouter({
routes
})
new Vue({
el: '#app',
template: '<App/>',
components: { App },
router
}).$mount('#app')
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
App.vue
<template>
<div>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
line-height: 1.4;
background-color: aliceblue;
}
</style>
components/Home.vue
<template>
<div id="app">
<Header />
<Posts v-bind:posts="posts" />
</div>
</template>
<script>
import Posts from './Posts.vue'
import Header from './Header.vue'
export default {
name: 'home',
components: {
Header, Posts
},
data() {
return {
posts: []
}
}
}
</script>
<style>
</style>
Let me know if I need to include Header.vue and Posts.vue too - I've omitted them because they were rendering fine before I tried to use vue-router.
How do I get rid of these console errors so my home page renders correctly when using vue-router? I have searched around and so far, all of my search results have led me to some variation on "make sure router is lowercase" (it is), "make sure routes is lowercase" (it is), and "use import Vue from 'vue/dist/vue.js'" (didn't work).
.$mount('#app')sinceel:"#app"does the job and also removeid="app"from<div id="app">insideApp.vue<div id="app"></div>that everything is meant to be inserted into (from index.html) is missing when I look at the Elements on my page - see my screenshot here vs here. Will this have anything to do with it? I'm not sure why the div is disappearing...main.jsyou're creating twoAppcomponents and mounting them at#app. You only need the first—I assume that's your issue.herenew Vuesection (the first one) and putting the router in there. I have no idea why this is now working without all theel: '#app', template: '<App/>', components: { App }stuff though, so if you added an answer which explains that I'd be happy to accept it (as it was your comment which led me to fix my issue) - or feel free to edit my answer to contain an explanation. Otherwise, I appreciate everyone's help on this.