Home > WeChat Applet > Mini Program Development > How to develop small programs using Taro + Vue3? (practice)

How to develop small programs using Taro + Vue3? (practice)

青灯夜游
Release: 2022-01-13 10:28:21
forward
6806 people have browsed it

How to use Taro3 Vue3 to develop small programs? The following article will introduce to you how to use Taro3 Vue3 to develop WeChat applet. I hope it will be helpful to you!

How to develop small programs using Taro + Vue3? (practice)

WeChat applet is an application that uses WeChat as the operating environment. Its essence is the application of Hybrid technology. Hybrid App is a mixed-mode mobile application. , so it is similar to H5, but has many native capabilities than H5, such as calling location information and cameras.

The development method of small programs is very similar to H5, and they also use JavaScript, HTML, CSS languages.

Therefore, small program development can be said to be a skill that a front-end engineer must master.

There is a certain learning cost in developing native mini programs. Nowadays, there are many third-party multi-terminal frameworks for developing mini programs on the market. If you are not pursuing the ultimate performance and stability, it is better not to use native mini programs for development efficiency. too low.

Among the third-party multi-terminal frameworks, taro and uni-app are the most widely used. Generally speaking, when making technology selection, teams use react, just use taro, the team uses vue, and just use uni-app, there is no difference between the two, they are both very easy to use.

But many developers may not know that taro3.0 or above supports the use of vue. This article will introduce how to use Taro3 Vue3 to develop WeChat applets.

After I completed the construction of this project based on the information on the Internet, I developed a small program using this project. The development experience really surpassed all the projects I have developed in the past and was very smooth (maybe This is my first time writing a script setup for vue3, and it is really comfortable to use).

You can directly access this project github address clone and use.

Target function

  • Integrate vue3, use script setup syntax development
  • IntegrationTypescript
  • Code inspection and format optimization
  • Global state management
  • Mini program subcontracting configuration
  • Style encapsulation, compatible with notch screen and other style issues
  • http method Encapsulation

Main technology stack

  • Taro3
  • Vue3
  • TypeScript
  • NutUi
  • Pinia

When vue3 was first released, my enthusiasm for learning vue3 was directly discouraged due to the lack of suitable UI framework support. Until now, excellent frameworks such as quasar, element-plus, ant-design-vue have successively supported vue3, and many vue3 projects have been used in production environments. Only then did I realize that everyone was really using vue3.

For example, the project team next door to our company used vue3 for the reconstruction project. Only then did I realize that I was a little late in learning vue3 (tips: the front end is really too complicated)

NutUI is a JD-style mobile component library. It supports the use of Vue language to write applications that can be used on H5 and mini program platforms, helping developers improve development efficiency and development experience.

I learned about NutUI from Taro documentation. Taro officially recommends using NutUI for development. They all seem to be from the same development team on JD.com. I started using it with the mentality of giving it a try. , the user experience is not bad.

Pinia is a state management library for Vue, similar to Vuex, it is another state management solution for Vue, supporting Vue2 and Vue3.

The first time I came into contact with a front-end status management tool was a back-end management system of the company when I was an intern. It used dva. It was a torture and almost persuaded me to quit. I gradually became familiar with it, but whether I use redux or vuex, I still find it troublesome to write.

这次尝试使用 Pinia,用起来确实很舒服,符合直觉,易于学习 ,有点类似于 recoil,但没有 recoil 那么多的概念和 API,主体非常精简,极易上手。Pinia 快速入门

vscode 需安装插件

  • Eslint
  • Prettier
  • Volar

vetur相同,volar是一个针对 vue 的 vscode 插件,不过与 vetur 不同的是,volar 提供了更为强大的功能。

Volar 介绍

搭建项目架构

初始化项目

初始化项目之前,需安装 taro,请参考 Taro 文档,完成 taro 安装

使用命令创建模板项目:

taro init myApp
Copy after login

How to develop small programs using Taro + Vue3? (practice)

安装 cli 用来执行构建等操作,之后启动项目,会生成一个 dist 目录

yarn add @tarojs/cli
yarn dev:weapp
Copy after login

打开微信开发工具 工程目录需要指向构建出来的 dist 文件

How to develop small programs using Taro + Vue3? (practice)

How to develop small programs using Taro + Vue3? (practice)

Hello world 出现,项目成功跑起来了!

设置代码规范

  • 代码规范 ESlint
  • 代码格式化 Prettier
  • 提交前检查 husky

个人认为,eslint + prettier 足以应付大部分前端代码规范问题了,且配置起来很简单,有特殊需求也可继续配置。

安装依赖

yarn add @vue/eslint-config-prettier @vue/eslint-config-typescript eslint-plugin-prettier vue-tsc husky -D
Copy after login

设置代码规范和格式化规则

.eslintrc.js

module.exports = {
  root: true,

  env: {
    node: true,
    'vue/setup-compiler-macros': true
  },

  extends: ['plugin:vue/vue3-essential', 'eslint:recommended', '@vue/prettier', '@vue/typescript'],

  parserOptions: {
    parser: '@typescript-eslint/parser'
  },

  rules: {
    'prettier/prettier': [
      'error',
      {
        singleQuote: true,
        semi: false,
        trailingComma: 'none',
        arrowParens: 'avoid',
        printWidth: 100
      }
    ],
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
}
Copy after login

.prettierrc

{
  "tabWidth": 2,
  "singleQuote": true,
  "semi": false,
  "trailingComma": "none",
  "arrowParens": "avoid",
  "endOfLine": "auto",
  "printWidth": 100
}
Copy after login

在 package.json 中 script 添加 Ts 检查命令和 Eslint 检查命令

"scripts":{
  "tsc": "vue-tsc --noEmit --skipLibCheck",
  "lint": "eslint --ext .vue --ext .js --ext .ts src/"
}
Copy after login

添加 husky 触发 Git 钩子,代码提交前检查

npx husky install
Copy after login

编辑 pre-commit 执行 Eslint 检查和 Ts 检查

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

echo "---eslint start---"
npm run lint
echo "---eslint end---"

echo "---ts lint start---"
npm run tsc
echo "---ts lint end---"
Copy after login

至此,项目的代码规范和格式规范配置完毕,多人协作也不是问题了。

引入 NutUI

yarn add @nutui/nutui-taro
Copy after login

.babelrcbabel.config.js 中添加配置:

module.exports = {
  // ...
  plugins: [
    [
      'import',
      {
        libraryName: '@nutui/nutui',
        libraryDirectory: 'dist/packages/_es',
        camel2DashComponentName: false
      },
      'nutui3-vue'
    ],
    [
      'import',
      {
        libraryName: '@nutui/nutui-taro',
        libraryDirectory: 'dist/packages/_es',
        camel2DashComponentName: false
      },
      'nutui3-taro'
    ]
  ]
}
Copy after login

按需引入,安装插件 babel-plugin-import

yarn add babel-plugin-import -D
Copy after login

样式处理 因为 nutui 的设计稿是 375 的 所以将框架的设计尺寸调整为 375

项目配置文件 config/index.js 中配置:

designWidth: 375
Copy after login

app.ts

import { createApp } from 'vue'
import { Button } from '@nutui/nutui-taro'

const app = createApp()

app.use(Button)
Copy after login

index.vue 中,nut-button 组件直接在 template 中写,不用再引入

<template>
  <view class="index">
    <text>{{ msg }}</text>
    <nut-button type="primary">主要按钮</nut-button>
  </view>
</template>
Copy after login

How to develop small programs using Taro + Vue3? (practice)

说实话,配置起来还是有点麻烦,不过按照官网文档说明来配也没有踩坑,还行。

小程序分包配置

小程序主包超过 2M,就无法真机预览了,为了提前做好准备在一开始就进行分包处理。比如下面这个小程序的配置,分了四个包。

app.config.ts

pages: [&#39;pages/create/index&#39;, &#39;pages/find/index&#39;, &#39;pages/my/index&#39;],
subpackages: [
{
  root: &#39;pages/featureA&#39;,
  pages: [&#39;index/index&#39;]
},
{
  root: &#39;pagesSub/search&#39;,
  pages: [&#39;index&#39;]
},
{
  root: &#39;pagesSub/my&#39;,
  pages: [&#39;detail/index&#39;, &#39;about/index&#39;]
},
{
  root: &#39;pagesSub/book&#39;,
  pages: [&#39;detail/index&#39;, &#39;person/list/index&#39;, &#39;person/detail/index&#39;]
}
],
Copy after login

可以在小程序开发工具编辑器里的代码依赖分析,查看主包和分包的大小

How to develop small programs using Taro + Vue3? (practice)

使用 script setup 语法封装小程序页面生命周期方法

hooks/life.ts

import { getCurrentInstance } from &#39;@tarojs/taro&#39;
import { onMounted } from &#39;vue&#39;

const Current = getCurrentInstance()

export function useDidShow(callback) {
    onMounted(callback) Current?.page?.onShow && (Current.page.onShow = callback)
}
export function usePullDownRefresh(callback) {
    Current?.page?.onPullDownRefresh && (Current.page.onPullDownRefresh = callback)
}
Copy after login

使用

import { useDidShow } from &#39;@/hooks/life&#39;

useDidShow(() => {
  // console.log(&#39;onShow&#39;)
})
Copy after login

安装 Pinia 进行状态管理

yarn add pinia
yarn add taro-plugin-pinia
Copy after login

项目配置文件 config/index.js 中配置:

plugins: [&#39;taro-plugin-pinia&#39;]
Copy after login

以管理用户信息和用户登录状态为例,实现一个用户登录功能

How to develop small programs using Taro + Vue3? (practice)

需要处理的文件代码如下:

stores/auth.ts

import { defineStore } from &#39;pinia&#39;

interface UserInfoProp {
  nickName: string
  avatarUrl: string
}

const useAuth = defineStore({
  id: &#39;authInfo&#39;,
  state: () => ({
    userInfo: {
      nickName: &#39;&#39;,
      avatarUrl: &#39;&#39;
    },
    isLogin: false
  }),
  actions: {
    login() {
      this.isLogin = true
    },
    logout() {
      this.isLogin = false
    },
    setUserInfo(userInfo: UserInfoProp) {
      this.userInfo = userInfo
    }
  }
})
export { useAuth }
Copy after login

stores/index.ts

import { createPinia } from &#39;pinia&#39;
import { useAuth } from &#39;./auth&#39;

export const store = createPinia()

const storeObj = {
  auth: useAuth
}

// 封装成useStore的形式,这样一看引用就知道是store的数据
export function useStore(key: string) {
  return storeObj[key]()
}
Copy after login

个人中心 index.vue

<template>
  <main v-if="isLogin">
    <user-info />
  </main>
  <main v-else>
    <nut-button type="primary" @click="handleLogin">微信一键登录</nut-button>
  </main>
</template>

<script setup>
import Taro from &#39;@tarojs/taro&#39;
import { computed } from &#39;vue&#39;
import { useStore } from &#39;@/stores&#39;

import UserInfo from &#39;./userInfo.vue&#39;

const auth = useStore(&#39;auth&#39;)
const isLogin = computed(() => auth.isLogin)

const handleLogin = () => {
  setTimeout(() => {
    // 模拟后端请求得到token和userInfo
    Taro.setStorageSync(&#39;token&#39;, &#39;xxxx&#39;)
    auth.setUserInfo({
      nickName: &#39;林&#39;,
      avatarUrl:
        &#39;https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png&#39;
    })
    auth.login()
  }, 500)
}
</script>

</script>
Copy after login

userInfo 组件

<template>
  <article>
    <nut-avatar size="large" :icon="userInfo.avatarUrl"></nut-avatar>
    <span class="ellipsis name">{{ userInfo.nickName }}</span>
  </article>
</template>

<script setup>
import Taro from &#39;@tarojs/taro&#39;
import { computed } from &#39;vue&#39;
import { useStore } from &#39;@/stores&#39;

const auth = useStore(&#39;auth&#39;)
const userInfo = computed(() => auth.userInfo)

</script>
Copy after login

总的来说, pinia 写起来是非常简洁的,这种类 react hooks 的写法,我是非常喜欢的

请求方法封装

http.ts

// 封装axios的请求,返回重新封装的数据格式
// 对错误的统一处理
import { HttpResponse } from &#39;@/common/interface&#39;
import Taro from &#39;@tarojs/taro&#39;
import publicConfig from &#39;@/config/index&#39;
import axios, {
  AxiosInstance,
  AxiosRequestConfig,
  AxiosResponse,
  Canceler
} from &#39;axios-miniprogram&#39;
import errorHandle from &#39;../common/errorHandle&#39;
const CancelToken = axios.CancelToken

class HttpRequest {
  private baseUrl: string
  private pending: Record<string, Canceler>

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl
    this.pending = {}
  }

  // 获取axios配置
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        &#39;Content-Type&#39;: &#39;application/json;charset=utf-8&#39;
      },
      timeout: 10000
    }
    return config
  }

  removePending(key: string, isRequest = false) {
    if (this.pending[key] && isRequest) {
      this.pending[key](&#39;取消重复请求&#39;)
    }
    delete this.pending[key]
  }

  // 设定拦截器
  interceptors(instance: AxiosInstance) {
    instance.interceptors.request.use(
      config => {
        console.log(&#39;config :>> &#39;, config)
        let isPublic = false
        publicConfig.publicPath.map(path => {
          isPublic = isPublic || path.test(config.url || &#39;&#39;)
        })
        const token = Taro.getStorageSync(&#39;token&#39;)
        if (!isPublic && token) {
          config.headers.Authorization = &#39;Bearer &#39; + token
        }
        const key = config.url + &#39;&&#39; + config.method
        this.removePending(key, true)
        config.cancelToken = new CancelToken(c => {
          this.pending[key] = c
        })
        return config
      },
      err => {
        errorHandle(err)
        return Promise.reject(err)
      }
    )

    // 响应请求的拦截器
    instance.interceptors.response.use(
      res => {
        const key = res.config.url + &#39;&&#39; + res.config.method
        this.removePending(key)
        if (res.status === 200) {
          return Promise.resolve(res.data)
        } else {
          return Promise.reject(res)
        }
      },
      err => {
        errorHandle(err)
        return Promise.reject(err)
      }
    )
  }

  // 创建实例
  request(options: AxiosRequestConfig) {
    const instance = axios.create()
    const newOptions = Object.assign(this.getInsideConfig(), options)
    this.interceptors(instance)
    return instance(newOptions)
  }

  get(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse> | Promise<HttpResponse> {
    const options = Object.assign(
      {
        method: &#39;get&#39;,
        url: url
      },
      config
    )
    return this.request(options)
  }

  post(url: string, data?: unknown): Promise<AxiosResponse> | Promise<HttpResponse> {
    return this.request({
      method: &#39;post&#39;,
      url: url,
      data: data
    })
  }
}

export default HttpRequest
Copy after login

request.ts

import HttpRequest from &#39;./http&#39;
import config from &#39;@/config/index&#39;
const baseUrl = process.env.NODE_ENV === &#39;development&#39; ? config.baseUrl.dev : config.baseUrl.pro

const request = new HttpRequest(baseUrl)

export default request
Copy after login

以获取图书列表和图书详情为例

apis/book.ts

import request from &#39;../request&#39;

export function getBookList() {
  return request.get(&#39;books/getBookList&#39;)
}

export function getBookDetail(id: number) {
  return request.post(&#39;books/getBookDetail&#39;, {
    id
  })
}
Copy after login

请求方法封装还是用到了 axios,只是用的是 axios-miniprogram ,写法和 web 端基本一致,http.js 文件引用的一些模块太多,本文没有列出来,可以直接访问本项目 github 地址查看。

样式封装

iPhoneX 底部横线适配

assets/styles/common.scss

.safe-area-bottom {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}
Copy after login

刘海儿屏适配

assets/styles/hairline.scss

@mixin hairline-common() {
  position: absolute;
  box-sizing: border-box;
  content: &#39; &#39;;
  pointer-events: none;
}

@mixin hairline() {
  @include hairline-common();
  top: -50%;
  right: -50%;
  bottom: -50%;
  left: -50%;
  border: 0 solid #eaeaea;
  transform: scale(0.5);
}

@mixin hairline-top($color, $left: 0, $right: 0) {
  @include hairline-common();
  top: 0;
  right: $right;
  left: $left;
  border-top: 1px solid $color;
  transform: scaleY(0.5);
}

@mixin hairline-bottom($color, $left: 0, $right: 0) {
  @include hairline-common();
  right: $right;
  bottom: 0;
  left: $left;
  border-bottom: 1px solid $color;
  transform: scaleY(0.5);
}

[class*=&#39;van-hairline&#39;] {
  &::after {
    @include hairline();
  }
}

.van-hairline {
  &,
  &--top,
  &--left,
  &--right,
  &--bottom,
  &--surround,
  &--top-bottom {
    position: relative;
  }

  &--top::after {
    border-top-width: 1px;
  }

  &--left::after {
    border-left-width: 1px;
  }

  &--right::after {
    border-right-width: 1px;
  }

  &--bottom::after {
    border-bottom-width: 1px;
  }

  &,
  &-unset {
    &--top-bottom::after {
      border-width: 1px 0;
    }
  }

  &--surround::after {
    border-width: 1px;
  }
}
Copy after login

多行文字省略

assets/styles/ellipsis.scss

@mixin multi-ellipsis($lines) {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: $lines;
  -webkit-box-orient: vertical;
}

@mixin ellipsis() {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.ellipsis {
  @include ellipsis();
}

.multi-ellipsis--l2 {
  @include multi-ellipsis(2);
}

.multi-ellipsis--l3 {
  @include multi-ellipsis(3);
}
Copy after login

总结

至此,终于完成了 Taro + Vue3 的项目搭建,强烈建议直接访问项目 github 地址 clone 使用,有一些配置细节本文无法一一列举,就在项目中去发掘吧!

如果我的文章能帮助到你,那将是我的荣幸!

【相关学习推荐:小程序开发教程

The above is the detailed content of How to develop small programs using Taro + Vue3? (practice). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template