简体   繁体   English

类型错误:无法读取未定义的属性“替换” - VueJS

[英]TypeError: Cannot read property 'replace' of undefined - VueJS

I am trying to implement a simple authentication in vuejs by following this tutorial我正在尝试按照本教程在 vuejs 中实现简单的身份验证

But when I go to my browser I am getting this error in my console "TypeError: Cannot read property 'replace' of undefined".但是当我转到浏览器时,我在控制台中收到此错误“类型错误:无法读取未定义的属性‘替换’”。 I have attached the screenshot of the same.我附上了相同的截图。 在此处输入图片说明

Here is my App.vue file这是我的 App.vue 文件

 <template> <div id="app"> <div id="nav"> <router-link v-if="authenticated" to="/login" v-on:click.native="logout()" replace>Logout</router-link> </div> <router-view @authenticated="setAuthenticated" /> </div> </template> <script> export default { name: 'App', data() { return { authenticated: false, mockAccount: { username: "nraboy", password: "password" } } }, mounted() { if(!this.authenticated) { this.$router.replace({ name: "login" }); } }, methods: { setAuthenticated(status) { this.authenticated = status; }, logout() { this.authenticated = false; } } } </script> <style> body { background-color: #F0F0F0; } h1 { padding: 0; margin-top: 0; } #app { width: 1024px; margin: auto; } </style>

Here is my Login component.这是我的登录组件。

 <template> <div id="login"> <h1>Login</h1> <input type="text" name="username" v-model="input.username" placeholder="Username" /> <input type="password" name="password" v-model="input.password" placeholder="Password" /> <button type="button" v-on:click="login()">Login</button> </div> </template> <script> /* eslint-disable no-console */ export default { name: 'Login', data() { return { input: { username: "", password: "" } } }, methods: { login() { if(this.input.username !== "" && this.input.password !== "") { if(this.input.username === this.$parent.mockAccount.username && this.input.password === this.$parent.mockAccount.password) { this.$emit("authenticated", true); this.$router.replace({ name: "secure" }); } else { console.log("The username and / or password is incorrect"); } } else { console.log("A username and password must be present"); } } } } </script> <style scoped> #login { width: 500px; border: 1px solid #CCCCCC; background-color: #FFFFFF; margin: auto; margin-top: 200px; padding: 20px; } </style>

And here is my router这是我的路由器

 import Vue from 'vue' import Router from 'vue-router' import LoginComponent from "./components/Login" import SecureComponent from "./components/Secure" Vue.use(Router) export default new Router({ routes: [ { path: '/', redirect: { name: "login" } }, { path: "/login", name: "login", component: LoginComponent }, { path: "/secure", name: "secure", component: SecureComponent } ] })

Since I am new to vue I am not able to figure out the exact cause of this error.由于我是 vue 的新手,我无法找出此错误的确切原因。 Any help is deeply appreciated.任何帮助深表感谢。

It seems that your router is not working.您的路由器似乎无法正常工作。 In your main.js :在您的main.js

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

Add dependencies:添加依赖项:

npm install --save vue-router 

it worked for my project它适用于我的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM