4

I'm new to vue.js and made this Landing component, which is linked to Login component. What I want to acheive is that when user clicks Login, the login page show up.

<template>
        <div>
            <div class="landing">
                <router-link to="/login">Login</router-link>
                </div>
            </div>
        </div>
    </template>

    <script>

    export default {
        name: 'Landing',
        data: function () {
            return {
            }
        },
        methods:  {

        }
    }
    </script>

The main.js:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Materials from "vue-materials"

import Routes from './routes'

const router = new VueRouter({
    routes: Routes,
    mode: 'history'
});


Vue.use(Materials)
Vue.use(VueRouter);

Vue.config.productionTip = false

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

App.Vue :

<template>
  <div id="app">
        <head>
          <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
          <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.css" rel="stylesheet">
        </head>

        <NavbarComp/>        
        <Landing/>
        <FooterComp/>
  </div>
</template>

<script>
import NavbarComp from './components/Navbar.vue';
import FooterComp from './components/Footer.vue';
import Landing from './components/Landing.vue';
import Login from './components/Login.vue';
import Register from './components/Register.vue';

export default {
  name: 'app',
  components: {
    NavbarComp,
    Landing,
    FooterComp,
    Login,
    Register
  }
}
</script>

routes.js:

import Login from './components/Login.vue'; 
import Register from './components/Register.vue'; 
import Landing from './components/Landing.vue'; 

export default [
  {path: '/login', component: Login, name: 'Login'},
  {path: '/register', component: Register, name: 'Register'},
  {path: '/', component: Landing, name: 'landing'},
]

And finally, Login.vue:

<template>  
  <div>
     <h2>Login</h2>    
   </div>

</template>

<script>

import axios from 'axios';

export default {
  name: 'Login',
  data: function () {
      return {
        ok: true,
        showErrorRegister: false,
        showErrorLogin: false,
        username: "",
        password: "",
        email: "",
        error: "",            
      }   
  },

When I click on Login link, the link in the URL bar changes but the component does not appear, and I don't see any error in the console. So don't know where to go from here.

How can I fix this?

10
  • No, I don't see any error. Commented Sep 5, 2018 at 2:53
  • This is a long shot, but if that is exactly what your Login.vue contents are, you are missing a closing curly brace and have an extra comma at the end there. And a missing </script>.. If the SFC isn't compiling it might cause it to not show up. Commented Sep 5, 2018 at 2:54
  • Well, actually I stripped the components from irrelevant axiom code, so perhaps have missed a curly brace here and there, but I see no error, they should be fine. Commented Sep 5, 2018 at 2:55
  • Does your stripped down version work in your environment? I would strip it down as much as you can to see if you can get it working with just the bare minimum. Commented Sep 5, 2018 at 2:57
  • 1
    Then you are looking at dynamic components here. Use <component :is=ComponentName> vuejs.org/v2/guide/components.html#Dynamic-Components Commented Sep 5, 2018 at 3:32

1 Answer 1

8

To elaborate on Cory's comment - which I believe to be the issue. You are missing <router-view>.

Your app currently appears to work because in App.Vue you are rendering the <Landing/> component. You should replace <Landing/> with <router-view/>. That way, when the route path is "/", the <router-view> will render the <Landing/> component in that space - much as it does now. When the route path is changed (by router-link) to "/Login", the Router will render the <Login/> component in that space instead.

Currently the Router is pointing to the correct Login component, however has nowhere to render it.

Read more here: https://router.vuejs.org/guide/#html

Sign up to request clarification or add additional context in comments.

3 Comments

Awsome, thanks for the tip. This resolved the problem.
No problem, glad it helped.
This confused me so much, but this answer works. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.