Vue.js 中使用 switch case 语句打印不同消息
<template>
<div>
<button @click="printMessage('hello')">打印'hello'</button>
<button @click="printMessage('world')">打印'world'</button>
<button @click="printMessage('vue')">打印'vue'</button>
</div>
</template>
<script>
export default {
methods: {
printMessage(message) {
switch (message) {
case 'hello':
console.log('你好!');
break;
case 'world':
console.log('世界!');
break;
case 'vue':
console.log('Vue.js!');
break;
default:
console.log('未知消息!');
break;
}
}
}
}
</script>
<p>在上面的示例中,我们在模板中添加了三个按钮,每个按钮都会触发<code>printMessage</code>方法。在该方法中,我们使用switch case语句来判断传递给方法的消息是什么,并打印相应的消息。如果传递的消息不是'hello'、'world'或'vue',则打印'未知消息'。</p>
原文地址: https://www.cveoy.top/t/topic/mkei 著作权归作者所有。请勿转载和采集!