Augusto
Augusto

Reputation: 4233

VueJS not open home page with "/" route

I want to show a simple home page with a h1 text, but it doesn't work. I configured route and added it to main.js. When I browse to localhost:8080 nothing is showing. I can't see the reason to it not works.

main.js

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

Vue.config.productionTip = false

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

App.vue

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

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

Home.vue

<template>
    <div>
        <h1>Home</h1>
    </div>
</template>

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

/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'

Vue.use(Router)

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Home',
            component: Home
        }
    ]
})

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title>client</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but client doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

Upvotes: 2

Views: 6281

Answers (1)

Turtlefight
Turtlefight

Reputation: 10680

@vue/cli per default only uses the vue runtime, but not the vue compiler ( the compiler is needed if you want to use inline template strings)

To work around this you can either turn on the runtimeCompiler in your vue.config.js:

module.exports = {
  "runtimeCompiler": true
};

Or change your code to use the render() function instead:

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

to

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

Btw if you're using @vue/cli, you can choose the Manually select features during vue create.
When you select Router as a feature you will get an project bootstrapped with vue-router that will work out of the box.

Upvotes: 1

Related Questions