最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - variable is assigned a value but never used when declaring it in Vue data object and using it in a method - Stack O

programmeradmin0浏览0评论

I'm getting an error in the webpack cmd window when I run npm run dev on a project.

Here is the code and the error message that I get specifically, the code related to the parent top-level Vue ponent, which has the navbar in that has details that change depending on if the user is logged in:

The code

<script>
// import required ponents
import EventBus from './ponents/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>

And the error message

  ✘    'decoded' is assigned a value but never used
  src\App.vue:60:11
      const decoded = null

To me, it looks like decoded is used in login(), any ideas?

I'm getting an error in the webpack cmd window when I run npm run dev on a project.

Here is the code and the error message that I get specifically, the code related to the parent top-level Vue ponent, which has the navbar in that has details that change depending on if the user is logged in:

The code

<script>
// import required ponents
import EventBus from './ponents/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>

And the error message

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

To me, it looks like decoded is used in login(), any ideas?

Share Improve this question edited Apr 7, 2019 at 6:04 Masuk Helal Anik 2,28323 silver badges31 bronze badges asked Apr 7, 2019 at 5:24 Adam ColeAdam Cole 1731 gold badge3 silver badges13 bronze badges 3
  • 1 Well you're not using decoded - it looks like a local variable in data. – Jack Bashford Commented Apr 7, 2019 at 5:26
  • 2 1) decoded is not part of the data return value so it won't be available in this.decoded. The unused local variable is why you get a warning 2) You have multiple return statements in data. Nothing after the first will be evaluated. 3) You don't want to try calling login() in your data function. Move it to created or mounted – Phil Commented Apr 7, 2019 at 5:29
  • @Phil Thanks. Sorry for my ignorance, but how do i access decoded whilst also declaring it as a const with the value of the decoded token? – Adam Cole Commented Apr 7, 2019 at 5:33
Add a ment  | 

1 Answer 1

Reset to default 7

you need to change in your data method

As your data is a function and what gets exposed is return value. you need to return decoded from data()in order to use decoded in your login method.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>