javascript - 类型错误 : Cannot read property 'replace' of undefined - VueJS

标签 javascript authentication vue.js vue-router

我正在尝试通过以下 this tutorial 在 vuejs 中实现简单的身份验证

但是当我进入浏览器时,我的控制台中出现此错误“TypeError:无法读取未定义的属性‘替换’”。我已附上相同的屏幕截图。 enter image description here

这是我的 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>

这是我的登录组件。

<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>

这是我的路由器

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
        }
    ]
})

由于我是 vue 新手,我无法找出此错误的确切原因。非常感谢任何帮助。

最佳答案

您的路由器似乎无法正常工作。在您的 main.js 中:

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

关于javascript - 类型错误 : Cannot read property 'replace' of undefined - VueJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55597558/

相关文章:

javascript - 带 ionic 切换而不是按钮的 ionic 输入插件

javascript - Ember {{#with ...controller=...}} 给出 "Uncaught TypeError: Cannot read property ' LookupFactory' 未定义”

unit-testing - 通过 SSL 测试安全登录和注册

c# - 如何以编程方式在 .NET Web 服务的 IIS 中设置集成 Windows 身份验证?

PHP/MySQL : Login form doesn't work

vue.js - Vuex 无法读取未定义的属性

vue.js - 如何访问 store/index.js 中导出之外的状态?

javascript - React - 使用同一类渲染多个组件 - 不变违规

javascript - 比较相等的对象给出错误,javascript

vue.js - vuex 模块还需要命名空间吗?