Ker*_*DER 5 javascript vue.js vue-resource vue-component
我想获得按钮单击事件的href属性.
<a v-on:click.prevent="func($event)" href="/user/all/2">
<i class="fa fa-edit"></i>
<span>Get Data</span>
</a>
Run Code Online (Sandbox Code Playgroud)
Main.JS文件
new Vue({
el: 'body',
methods: {
func: function (event) {
element = event.target;
console.log(element); // Output : Select span|i|a element
href = element.getAttribute('href');
},
}
});
Run Code Online (Sandbox Code Playgroud)
目标事件不会选择元素.它选择单击的元素.
Bil*_*ell 14
你想要的event.currentTarget不是event.target.这是一个小提琴的情况:https://jsfiddle.net/crswll/553jtefh/
小智 5
这是“ Vue之道”。Vue与可重用组件有关。因此,首先创建组件:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp></my-comp>
</div>
<script>
// register component
Vue.component('my-comp', {
template: '<div>Just some text</div>'
})
// create instance
new Vue({
el: '#app'
})
</script>Run Code Online (Sandbox Code Playgroud)
现在添加自定义属性并读取其值:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp my-attr="Any value"></my-comp>
</div>
<script>
Vue.component('my-comp', {
template: '<div>aaa</div>',
created: function () {
console.log(this.$attrs['my-attr']) // And here is - in $attrs object
}
})
new Vue({
el: '#app'
})
</script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17733 次 |
| 最近记录: |