变量被分配了一个值,但在 Vue 数据对象中声明它并在方法中使用它时从未使用过

Ada*_*ole 1 javascript eslint vue.js

当我npm run dev在项目上运行时,webpack cmd 窗口中出现错误。

这是我特别收到的代码和错误消息,与父顶级 Vue 组件相关的代码,其中包含导航栏的详细信息,具体取决于用户是否登录:

编码

<script>
// import required components
import EventBus from './components/EventBus'
import router from './router/index.js'
import jwtDecode from 'jwt-decode'

export default {
  data () {
    const token = localStorage.usertoken
    const decoded = jwtDecode(token)
    return {
      first_name: '',
      surname: '',
      email: '',
      created: ''
    }

    return {
      auth: false
    }

    try {
      this.login()
    } catch (error) {
      console.log('Not currently signed in')
    }
  },

  methods: {
    logout () {
      this.first_name = ''
      this.surname = ''
      this.email = ''
      this.created = ''
      localStorage.removeItem('usertoken')
      this.auth = false
      router.push({
        name: 'login'
      })
    },

    login () {
      this.first_name = this.decoded.f_name
      this.surname = this.decoded.s_name
      this.email = this.decoded.email
      this.created = this.decoded.created
    }
  },

  mounted () {
    EventBus.$on('logged-in', status => {
      this.auth = status
      this.login()
    })
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

和错误信息

  ?  http://eslint.org/docs/rules/no-unused-vars  'decoded' is assigned a value but never used
  src\App.vue:60:11
      const decoded = null
Run Code Online (Sandbox Code Playgroud)

对我来说,它看起来像是decoded用于login(),有什么想法吗?

小智 6

你需要改变你的数据方法

由于您的数据是一个函数,而暴露的是返回值。您需要从 data() 返回解码后才能在登录方法中使用解码。

 data () {
        const token = localStorage.usertoken
        const decoded = jwtDecode(token)
        return {
          first_name: '',
          surname: '',
          email: '',
          created: '',
          decoded: decoded
        }
Run Code Online (Sandbox Code Playgroud)