Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js router-link does not load component

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?

like image 659
Karlom Avatar asked Mar 05 '23 12:03

Karlom


1 Answers

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

like image 150
Zak Avery Avatar answered Mar 27 '23 16:03

Zak Avery