在项目上运行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()返回已解码,以便在登录方法中使用已解码。
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/