自己写的时候遇到的一点点小困难,做此记录,希望对大家有帮助
<!DOCTYPE html>
<html>
<head>
<title>Vue Example</title>
</head>
<body>
<div id="app">
<button v-for="button in buttons" @click="showContent(button.content)">{{ button.label }}</button>
<div v-if="selectedContent">{{ selectedContent }}</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
buttons: [
{ label: '按钮1', content: '内容1' },
{ label: '按钮2', content: '内容2' },
{ label: '按钮3', content: '内容3' }
],
selectedContent: null
},
methods: {
showContent(content) {
this.selectedContent = content;
}
}
});
</script>
</body>
</html>
在这个修改后的示例中,我们使用了`v-for`指令来动态渲染按钮,按钮的标签和内容通过`buttons`数组中的数据进行绑定。当点击按钮时,依然会触发`showContent`方法,根据按钮的`content`属性来显示不同的内容。