2017-06-13 51 views
1

這是我的組件。 el.focus()確實在插入時有效,但我在控制檯上得到了[Vue warn]: Property or method "v" is not defined on the instance but referenced during render。 如何清除警告?[Vue警告]:屬性或方法「v」未在實例上定義,但在渲染過程中引用

<script> 
 
    export default { 
 
    directives: { 
 
     focus: { 
 
     inserted (el) { 
 
      el.focus() 
 
     } 
 
     } 
 
    } 
 
    } 
 
</script>
<style lang="stylus" scoped> 
 
    input 
 
    width: 300px 
 
    height: 30px 
 
    border: 1px solid #000 
 
</style> 
 

 
<template lang="jade"> 
 
    div 
 
    input(v-focus) 
 
</template>

回答

2

當你傳遞一個空屬性玉,據我所知,是什麼解釋,以純HTML,是attribute="attribute"。在你的情況下,實際的HTML是v-focus="v-focus",vue將其解釋爲使用屬性v的表達式,該屬性不存在。

您可以嘗試input(v-focus="true"),因爲傳遞給該指令的值並不重要。

+0

非常感謝。我嘗試過'input(v-focus =「」)' –

+0

它工作嗎?如果是這樣,你可以接受答案。 –

+0

我在'inserted'函數中打印'binding'參數。 然後我發現: 當我的代碼是「輸入(v焦點)」,binding.expression是「v焦點」。 當我的代碼是'input(v-focus)'時,綁定沒有'expression'屬性,並且警告消失 –

相關問題