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
195 views
in Technique[技术] by (71.8m points)

vue+typescript无法使用this.$options.data()

export default class Test extends Vue {
    
    t = "测试"

    created() {
            console.log(this.$options.data())
            //Error in created hook: "TypeError: Cannot read property 'props' of undefined"
            console.log(this.$options.data.call(this))
            //{}   call的话 返回的是空对象,没有t
        }
 }   
    

放到普通的js版的vue 就没问题~~ 求大佬们帮助~万分感谢~


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

1 Answer

0 votes
by (71.8m points)

重置的话使用

Object.assign(this.$data, this.$options.data.call(this))
Object.assign(this.$data.target, this.$options.data.call(this).target)

vue-class-component对data做了包裹,导致this.$options.data没有返回我们想要的值


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

...