2017-04-06 57 views
1

如何才能在函數內調用函數?通過v-on調用函數內部功能:點擊

模板:(HTML)

<a v-on:click="hello.world"></a> 

JS:(作爲組分)

methods: { 
    hello: function() { 
     return { 
      world: function() { 
       alert('hello world'); 
      } 
     }; 
    } 
} 

這我得到了警告:

[Vue公司警告]:事件 「點擊」 無效的處理程序:得到未定義

這可能是通過JS 調用this.hello().world()但不是在模板(HTML)v-on

我錯過了什麼?

+2

使用它像這樣''foo是 –

+1

!!!!! ,應該是@BelminBedak的答案吧! – l2aelba

回答

1

我不認爲這是一個vue問題。

this.hello().world()無效,因爲world()現在無法訪問。試試在和hello功能的加入雙括號()

methods: { 
    hello: function() { 
    return { 
     world: function() { 
      alert('hello world'); 
     } 
    }(); 
    } 
} 

,您現在可以訪問這樣的內部函數:this.hello.world();

0

只要定義世界作爲一個函數或者內部的方法,在全球範圍還是在VUE組件並調用它在一個定期的時尚。

<a @click="hello">foo</a>

function world() { 
    alert("hello world"); 
} 

<vue component> { 
    methods: { 
    hello: function() { 
     world(); 
    } 
    } 
} 

或本

<vue component> { 
    methods: { 
    hello: function() { 
     this.world(); 
    }, 
    world: function() { 
     alert("Hello world"); 
    } 
    } 
}