Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

main.js断点看到 Vue 显示未定义,但是却可以打印,this打印undefined,断点却可以显示,这是什么情况?

main.js断点看到 Vue 显示未定义,但是却可以打印,Vue原型方法里面的this,同样断点,this指向vue打印出来是undefined 这是什么情况?

应该是没什么特殊的操作,就是单纯的打印

// main.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
...
console.log(Vuex)
console.log(Vuex)
const store=new Vuex.Store({
  state:{
    name:'title',
  },
}

Vue.prototype.pop = (n=-1) => {
  console.log(this)
  console.log(this)
  this.$router.go(n);
}

pop就是在组件里通过 this.pop() 调用

image.png

image.png

image.png

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

箭頭函數沒有自己的 this,它的 this 由它定義時候所處的詞法上下文決定,你改用普通函數試試。

Vue.prototype.pop = function (n = -1) {
  this.$router.go(n);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...