我是vue.js的新手,并制作了这个Landing
组件,该组件链接到Login
组件。我要实现的是,当用户单击“登录”时,将显示登录页面。
<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>
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'},
]
最后,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: "",
}
},
当我单击“登录”链接时,URL栏中的链接会更改,但是该组件没有出现,并且在控制台中没有看到任何错误。所以不知道从这里去哪里。
我该如何解决?
答案 0 :(得分:4)
详细阐述Cory的评论-我认为这是问题所在。您缺少<router-view>
。
您的应用当前似乎可以正常使用,因为在App.Vue中您正在渲染<Landing/>
组件。您应将<Landing/>
替换为<router-view/>
。这样,当路由路径为“ /”时,<router-view>
将在该空间中渲染<Landing/>
组件-就像现在一样。当路由路径(通过router-link)更改为“ / Login”时,路由器将改为在该空间中呈现<Login/>
组件。
当前路由器指向正确的登录组件,但是无处可呈现。
在此处了解更多信息:https://router.vuejs.org/guide/#html