Vue——props默认值为工厂函数时[工厂函数:undefined]问题解决方案
生活随笔
收集整理的這篇文章主要介紹了
Vue——props默认值为工厂函数时[工厂函数:undefined]问题解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述
methods: {sayHi(){alert('hi');} }, props: {keyword: {default: 'a'},say: {type: Function,default: this.sayHi} }?函數sqyHi未定義。
官方文檔
https://cn.vuejs.org/v2/guide/components-props.html?
問題分析
Vue?在初始化實例對象狀態的時候,prop?會在一個組件實例創建之前進行驗證,在?methods?和?data?之前,所以即使用工廠函數this也不指向組件實例
唯一能與methods共享function的方法是把function單獨寫在實例外
解決方案
function sayHi() {alert('hi'); }export default {methods: {sayHi,},props: {say: {type: Function,default: sayHi,}} }參考文章
https://segmentfault.com/q/1010000019746756/
總結
以上是生活随笔為你收集整理的Vue——props默认值为工厂函数时[工厂函数:undefined]问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript——获取浏览器滚动条
- 下一篇: C#——Ellipse(椭圆)类[继承E