账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Vue 引入 component Cannot read property 'esndir' of undefined
    28
    0

    <template>
    <div>

    <nav class="navbar navbar-default" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">ESN</a>
        </div>
        <button type="button" class="btn btn-default navbar-btn pull-right" v-on:click="logout()">
          Logout
        </button>
      </div>
    </nav>
    <div class="weclome-msg" v-show="showWelcomeMessage">
      <h1> Welcome {{ username }} to User home!!!</h1>
      <p> Got it, go to see DSN directory </p>
      <button v-on:click="seeDSNDir()">Yes</button>
    </div>
    <esndir v-show="showESNDir">
    </esndir>

    </div>
    </template>

    <script>
    import {setCookie, getCookie} from '../assets/js/cookie.js'
    import esndir from '@/components/esndir'
    import axios from 'axios'
    import io from 'socket.io-client'
    export default {
    data () {

    return {
      username: '',
      showWelcomeMessage: true,
      showESNDir: false
    }

    },
    mounted () {

    let cookieUsername = getCookie('username')
    let _this = this
    this.username = cookieUsername
    if (cookieUsername === '') {
      this.$router.push('/login')
    }
    this.socket = io.connect('http://localhost:3000')
    this.socket.emit('userLogin', this.username)
    this.socket.on('updateUser', function () {
      _this.$ref.esndir.getCitizens()
    })

    },
    methods: {

    seeDSNDir () {
      this.showWelcomeMessage = false
      this.showESNDir = true
    },
    logout () {
      let _this = this
      axios.post('/apis/users/logout', {
        params: {
          username: _this.username
        }
      })
        .then(function (response) {
          console.log(response)
          if (response.status === 200) { // remove current cookie
            _this.socket.emit('userLogout')
            setCookie('username', '', -1)
            _this.$router.push('/')
          }
        })
        .catch(function (error) {
          console.log(error)
        })
    }

    },
    components: {

    esndir

    }
    }
    </script>

    user.vue?9c2f:46 Uncaught TypeError: Cannot read property 'esndir' of undefined

    at Socket.eval (user.vue?9c2f:46)
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 妞,给爷笑一个 普通会员 1楼

      这个错误信息通常意味着你尝试访问一个未定义的属性或方法。在 Vue 中,我们可以通过 this 指针来访问组件的属性和方法,但如果你尝试访问一个未定义的属性或方法,Vue 就会抛出这个错误。

      例如,如果你在 mounted 生命周期钩子中尝试访问一个未定义的属性,那么就会出现这个错误。

      解决这个问题的一种方法是确保你在访问属性或方法之前,已经正确地获取了组件实例。例如:

      javascript this.$refs.myComponent.$el.style.color = 'red';

      在这个例子中,我们首先使用 this.$refs.myComponent 获取组件实例,然后使用 this.$el 访问 myComponent 组件的 DOM 元素。

      另一个解决这个问题的方法是在访问属性或方法之前,先检查它是否存在。例如:

      javascript if ('esndir' in this.$refs.myComponent) { console.log(this.$refs.myComponent.esndir); } else { console.log('myComponent esndir not found'); }

      在这个例子中,我们首先检查 esndir 属性是否存在,如果存在,我们就打印它的值,否则我们就打印一条错误消息。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部