Taro + vue3 小程序封装标题组件

本文介绍了如何在Vue和Taro框架下创建一个包含标题和返回功能的可复用title组件,提供基本样式和导航控制,以便于开发者根据需求进行定制。
摘要由CSDN通过智能技术生成

分为没有跳转页面的title组件和 有跳转页面的title组件

我们可以把这个封装成一个组件

直接上代码

<template>
  <div class="fixed-title-container">
    <div class="box">
      <div class="icon" v-if="isShow" @click="handleBack">
        <IconFont name="rect-left" size="12"></IconFont>
      </div>
      <div class="text" :style="{ marginLeft: isShow ? '15px' : '0' }">{{ Title }}</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, toRefs } from "vue";
import { IconFont } from "@nutui/icons-vue-taro";
import Taro from "@tarojs/taro";
const props = defineProps({
  Title: String,
  isShow: {
    required: false,
    default: false,
  },
});
const { Title, isShow } = toRefs(props);
const handleBack=()=>{
  Taro.navigateBack({
    delta: 1,
  })
}
</script>

<style lang="scss">
.fixed-title-container {
  height: 120px;
  padding: 0 30px;
  line-height: 120px;
  background: linear-gradient(
    to right,
    #d8ecfe,
    #d2e8fe,
    #cce5fe,
    #cde5ff,
    #c6e2ff,
    #c2dfff,
    #c1dffe
  );
  left: 0;
  right: 0;
  top: 0;
  z-index: 9;
  position: fixed;

  .box {
    display: flex;
    // justify-content: space-between;
    align-items: center;
    .icon {
     height: 50px;
     line-height: 50px;
     text-align: center;
      width: 50px;
      border-radius: 50%;
      background-color:d8ecfe;
      border: 1px solid #ccc;
    }
    .text {
      font-size: 30px;
      color: #15181d;
      font-weight: 700;
    }
  }
}
</style>
 <FixedTitle Title="功夫熊猫4" :isShow="true"></FixedTitle> //组件使用

当然我这个是比较简单的一个组件

这个组件的目的除了title标题 其实还有一个就是 返回上一页 类似我们小程序自己路由上的返回 

逻辑很简单的 大家可以在我这个基础上继续修改更加适合自己逻辑和样式的组件

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Taro + Vue 3 + TypeScript 的小程序项目中使用 Pinia,你可以按照以下步骤进行配置: 1. 首先,确保你已安装了 TaroVue 3 模板。你可以使用以下命令创建一个新的 Taro + Vue 3 + TypeScript 项目: ```bash npx @tarojs/cli@next init myApp --template vue3-ts ``` 2. 在项目目录下安装 Pinia: ```bash npm install pinia@2.0.23 ``` 3. 在 `src/main.ts` 文件中,导入 `createPinia` 并创建一个 Pinia 实例,并将其传递给 Vue 的 `createApp` 方法: ```typescript import { createApp } from 'vue' import { createPinia } from 'pinia' const app = createApp(...) const pinia = createPinia() app.use(pinia) ... ``` 4. 接下来,你可以根据需要创建和使用 Pinia 的 store。在 `src/store` 目录下创建一个新的文件来定义你的 store,例如 `counter.ts`: ```typescript import { defineStore } from 'pinia' export const useCounterStore = defineStore('counter', { state: () => ({ count: 0, }), actions: { increment() { this.count++ }, decrement() { this.count-- }, }, }) ``` 5. 在组件中使用 store。在你的组件中,你可以使用 `useStore` 函数来获取 store 的实例: ```typescript import { useCounterStore } from '../store/counter' export default { setup() { const counterStore = useCounterStore() return { counterStore, } }, } ``` 6. 最后,你可以在你的组件中使用 store: ```vue <template> <div> <p>Count: {{ counterStore.count }}</p> <button @click="counterStore.increment()">Increment</button> <button @click="counterStore.decrement()">Decrement</button> </div> </template> <script setup> import { useCounterStore } from '../store/counter' const counterStore = useCounterStore() </script> ``` 这样,你就可以在 Taro + Vue 3 + TypeScript 的小程序项目中使用 Pinia 了。如果你遇到任何问题,请提供更多的上下文和相关代码,我将尽力帮助你解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值