Guide引导页 vue3国际化实现

先看效果
在这里插入图片描述
思路
基于 driver.js 实现

1.npm i driver.js@0.9.8

模板代码
<SvgIcon icon="guide" @click="onClick" id="guide-start"/>
<script setup>
import Driver from 'driver.js'
import 'driver.js/dist/driver.min.css'
import { onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import steps from './steps'
const i18n = useI18n()

let driver = null
onMounted(() => {
  driver = new Driver({
    // 禁止点击蒙版关闭
    allowClose: false,
    closeBtnText: i18n.t('msg.guide.close'),
    nextBtnText: i18n.t('msg.guide.next'),
    prevBtnText: i18n.t('msg.guide.prev')
  })
})
const onClick = () => {
  driver.defineSteps(steps(i18n))
  driver.start()
}
</script>

2.创建步骤

步骤上面element的值就是对应组件上面的id 要在组件上面写上id就可

const steps = i18n => {
  return [
    {
      element: '#guide-start',
      popover: {
        title: i18n.t('msg.guide.guideTitle'),
        description: i18n.t('msg.guide.guideDesc'),
        position: 'bottom-right'
      }
    },
    {
      element: '#guide-hamburger',
      popover: {
        title: i18n.t('msg.guide.hamburgerTitle'),
        description: i18n.t('msg.guide.hamburgerDesc')
      }
    },
    {
      element: '#guide-breadcrumb',
      popover: {
        title: i18n.t('msg.guide.breadcrumbTitle'),
        description: i18n.t('msg.guide.breadcrumbDesc')
      }
    },
    {
      element: '#guide-search',
      popover: {
        title: i18n.t('msg.guide.searchTitle'),
        description: i18n.t('msg.guide.searchDesc'),
        position: 'bottom-right'
      }
    },
    {
      element: '#guide-full',
      popover: {
        title: i18n.t('msg.guide.fullTitle'),
        description: i18n.t('msg.guide.fullDesc'),
        position: 'bottom-right'
      }
    },
    {
      element: '#guide-theme',
      popover: {
        title: i18n.t('msg.guide.themeTitle'),
        description: i18n.t('msg.guide.themeDesc'),
        position: 'bottom-right'
      }
    },
    {
      element: '#guide-lang',
      popover: {
        title: i18n.t('msg.guide.langTitle'),
        description: i18n.t('msg.guide.langDesc'),
        position: 'bottom-right'
      }
    },
    {
      element: '#guide-tags',
      popover: {
        title: i18n.t('msg.guide.tagTitle'),
        description: i18n.t('msg.guide.tagDesc')
      }
    },
    {
      element: '#guide-sidebar',
      popover: {
        title: i18n.t('msg.guide.sidebarTitle'),
        description: i18n.t('msg.guide.sidebarDesc'),
        position: 'right-center'
      }
    }
  ]
}
export default steps

### Vue 3 简单页面引导组件示例 为了实现一个简单的页面引导功能,在 Vue 3 中可以创建自定义的引导组件来帮助新用户了解应用程序的主要特性。下面是一个基本的例子,展示了如何构建这样的组件。 #### 创建 `GuideComponent.vue` 此文件位于项目的 `components` 文件夹下: ```vue <template> <div v-if="showGuide"> <div class="overlay" @click.self="closeGuide"></div> <div id="guide-box" :style="{ top: position.top + &#39;px&#39;, left: position.left + &#39;px&#39; }"> <h4>{{ currentStep.title }}</h4> <p>{{ currentStep.description }}</p> <button @click="nextStep">{{ buttonText }}</button> </div> </div> </template> <script setup> import { ref, onMounted, computed } from &#39;vue&#39;; const props = defineProps({ steps: { type: Array, required: true } }); let stepIndex = ref(0); let showGuide = ref(true); function closeGuide() { showGuide.value = false; } function nextStep() { if (stepIndex.value === props.steps.length - 1) { closeGuide(); } else { stepIndex.value++; } } const currentStep = computed(() => props.steps[stepIndex.value]); const position = computed(() => ({ top: currentStep.value.position.y, left: currentStep.value.position.x })); const buttonText = computed(() => ( stepIndex.value === props.steps.length - 1 ? &#39;Finish Guide&#39; : &#39;Next&#39; )); </script> <style scoped> .overlay { width: 100%; height: 100vh; background-color: rgba(0, 0, 0, .7); z-index: 998; position: fixed; } #guide-box { padding: 2rem; border-radius: 5px; box-shadow: 0 0 10px black; color: white; font-size: large; text-align: center; background-color: #ffcccb; position: absolute; z-index: 999; } </style> ``` 上述代码实现了带有遮罩层的向导框,该框会按照传入的步骤数组依次显示不同的提示信息[^1]。 #### 使用 `GuideComponent` 可以在任何父级组件中引入并使用这个新的引导组件,通过传递一系列描述性的步骤对象给它作为属性值。这些步骤应该包含标题、描述文字以及相对于视口的位置坐标等信息。 ```html <!-- ParentComponent.vue --> <template> <!-- Other components... --> <GuideComponent :steps="tutorialSteps"/> <!-- ... --> </template> <script setup> // Import statements... const tutorialSteps = [ { title: "Welcome", description: "This is your dashboard.", position: { x: 100, y: 100 }, }, // Add more steps here... ]; </script> ``` 这样就完成了一个简易版的新手教程或产品介绍流程的设计。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值