2017-07-04 51 views
0

我已在我的nuxt.js安裝中使用vee-validator安裝(使用express template)。

我已經加入了插件安裝到我的nuxt.config.js文件像這樣:

plugins: [ 
    { src: '~plugins/vee-validate.js' } 
] 

我還增加了它到構建設置:

build: { 
    vendor: ['vee-validate'], 
    ... 

當我使用的HTML標籤如v-validate="'required'",驗證按預期工作,並且我可以在模板中顯示/隱藏錯誤。

現在我試圖設置一個表單,這樣當它被提交時,所有的字段都會被驗證,錯誤只會在嘗試提交後纔會顯示。要做到這一點,我已經建立了一個方法,像這樣:

methods: { 
    onLogin: (e) => { 
     e.preventDefault() 
     console.log('validator', this.$validator) 
    } 
    } 

出於某種原因,this.$validator是不確定的。有人可以告訴我怎樣才能訪問它嗎?我曾嘗試加入:

inject: ['$validator'] 

但這似乎沒有任何區別。

感謝您的任何幫助。

回答

0

原來問題在於使用ES6函數風格,習慣了使用Angular的風格!

更改:

methods: { 
    onLogin: (e) => { 
     e.preventDefault() 
     console.log('validator', this.$validator) 
    } 
} 

到:

methods: { 
    onLogin: function (e) { 
     e.preventDefault() 
     console.log('validator', this.$validator) 
    } 
} 

,現在它工作正常。

相關問題