2017-04-06 39 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"); 
    } 
    } 
}