javascript - 是否可以从主 Vue 实例的方法中添加/删除组件的道具?
问题描述
在 Vue.js 上,我有一个prop
名为loading的 v-button 组件。
零件:
Vue.component('v-button', {
props: ['loading'],
template: '<button class="btn btn-info"> <span v-if="loading != undefined">Loading...</span> <span v-else><slot></slot></span> </button>'
});
视图:
new Vue({
el: '#app',
components: ['v-button'],
methods: {
niceMethod: function(argument) {
console.log(argument);
// Considering that I will have a large amount of v-buttons:
// Is it possible to do something here to add or remove the prop "loading" ONLY on the v-button I've clicked?
}
}
})
身体:
<div id="app" class="p-5">
<p><v-button @click.native="niceMethod('testing!')">Test</v-button></p>
<p><v-button @click.native="niceMethod('hello!')">Hello</v-button></p>
<p><v-button @click.native="niceMethod('vue!')">Vuejs</v-button></p>
</div>
考虑到我会有大量的 v 按钮:是否可以在 Vue 实例上做一些事情来添加或删除仅在我单击的 v 按钮上加载的道具?
我错过了 Vue.js 文档上的内容吗?
对不起,如果我的问题不清楚......如果是这样,请检查我所做的JSFiddle 。
解决方案
为什么不在loading
组件内切换数据真/假?
Vue.component('v-button', {
template: '<button class="btn btn-info" @click="clickMe"> <span v-if="loading">Loading...</span> <span v-else><slot></slot></span> </button>',
data() {
return {
loading: false
}
},
methods: {
clickMe() {
this.loading = !this.loading
}
}
});
new Vue({
el: '#app',
components: ['v-button'],
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
<p><v-button>Test</v-button></p>
<p><v-button>Hello</v-button></p>
<p><v-button>Vuejs</v-button></p>
</div>
推荐阅读
- javascript - 如何将多个对象的数组合并为单个对象?
- vue.js - 为什么刷新时不调用beforedestory,而是调用created?
- apache-beam - 是否可以将复合触发器与带有数据流的微批处理结合使用?
- amazon-dynamodb - 带有 ADD 的 DynamoDb UpdateExpression 是否可以增加计数器的事务性?
- r - 尝试将离散随机变量提高到负幂时出错
- javascript - 如何通过读取 Nodejs 中的文件在 Bowser 中构造一个新文件?
- python - 如何在 PyQt5 LineEdit 小部件中刷新自动完成建议列表
- google-chrome - chrome 更新到版本 81.0.4044..111 后无法播放远程视频流
- mongodb - 适合上传 PDF 的云以及 react native 和 mern stack
- python - 从 scipy 子模块导入的正确方法