LangSwitch.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <template>
  2. <div class="lang-switch flex-center">
  3. <!-- <el-button @click="switchToZh" :type="langStore.currentLang === 'zh-CN' ? 'primary' : 'text'">中文</el-button>
  4. <el-button @click="switchToEn" :type="langStore.currentLang === 'en' ? 'primary' : 'text'">English</el-button> -->
  5. <el-dropdown @command="handleClick" type="primary" round>
  6. <span class="el-dropdown-link">
  7. <el-button size="small" round>{{ langStore.currentLang === 'zh-CN' ? 'ZN' : 'EN' }}</el-button>
  8. </span>
  9. <template #dropdown>
  10. <el-dropdown-menu>
  11. <el-dropdown-item command="zh-CN" v-if="langStore.currentLang !== 'zh-CN'">中文</el-dropdown-item>
  12. <el-dropdown-item command="en" v-if="langStore.currentLang !== 'en'">English</el-dropdown-item>
  13. </el-dropdown-menu>
  14. </template>
  15. </el-dropdown>
  16. </div>
  17. </template>
  18. <script setup>
  19. import { useLangStore } from '@/pinia/langStore'
  20. const langStore = useLangStore()
  21. // 点击切换语言
  22. const handleClick = (command) => {
  23. if (command === 'zh-CN') {
  24. switchToZh()
  25. } else if (command === 'en') {
  26. switchToEn()
  27. }
  28. }
  29. // 切换到中文
  30. const switchToZh = () => {
  31. langStore.changeLang('zh-CN')
  32. }
  33. // 切换到英文
  34. const switchToEn = () => {
  35. langStore.changeLang('en')
  36. }
  37. </script>
  38. <style scoped>
  39. .lang-switch {
  40. }
  41. </style>