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

标签 javascript vue.js eslint

当我在项目上运行 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>

错误信息

  ✘  http://eslint.org/docs/rules/no-unused-vars  'decoded' is assigned a value but never used
  src\App.vue:60:11
      const decoded = null

对我来说,decoded 似乎用在 login() 中,有什么想法吗?

最佳答案

你需要改变你的数据方法

因为你的数据是一个函数,暴露的是返回值。您需要从 data() 中返回 decoded,以便在您的登录方法中使用 decoded。

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

关于javascript - 变量被分配了一个值,但在 Vue 数据对象中声明它并在方法中使用它时从未使用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555883/

相关文章:

javascript - 使用map方法返回一组新的数组

vue.js - Vue-CLI 3 通过路径文件检查文件是否存在?

javascript - 类型错误 : Cannot read property 'push' of undefined [VueJs]

vue.js - 如何在 .vue 中使用 eslint 自动修复?

javascript - Eslint 在 VScode 中不工作,但在终端工作

javascript - ajax 请求不适用于 laravel 5.0

javascript - ES6 参数的默认值

javascript - 单击一下即可切换两个不同的 div

css - 在视差中心显示 div 的问题

javascript - Node.js错误: Each then() should return a value or throw?