<template>
  <div>
    <button @click="printMessage('hello')">Print Hello</button>
    <button @click="printMessage('world')">Print World</button>
  </div>
</template>
<script>
export default {
  methods: {
    printMessage(message) {
      switch (message) {
        case 'hello':
          console.log('Hello!');
          break;
        case 'world':
          console.log('World!');
          break;
        default:
          console.log('Invalid message!');
      }
    }
  }
}
</script>
<p>在这个例子中,我们在 Vue 组件中定义了一个 printMessage 方法,该方法接受一个字符串参数。然后,我们使用 switch case 语句来检查该参数的值,并根据其值打印不同的消息。最后,我们将该方法分别绑定到两个按钮的点击事件上,以测试它是否可以正常工作。</p>
Vue 中使用 switch case 语句打印内容

原文地址: https://www.cveoy.top/t/topic/mkef 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录