Taro an open cross-end cross-frame solution

Taro an open cross-end cross-frame solution

2022-09-02 0 482
Resource Number 38018 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

Taro recommended in this issue is an open cross-end cross-framework solution that supports the use of frameworks such as React/Vue/Nerv to develop applications such as wechat/Jingdong/Baidu/Alipay/Bytedance/QQ/Feishu applet/H5 / RN.

project context

Nowadays, the market has a variety of forms, and various terminals such as Web, React Native, and wechat mini programs are popular. When business requirements are required to perform at different ends at the same time, the cost of writing multiple sets of code for different ends is obviously very high, and the ability to write only one set of code can be adapted to multiple ends is extremely needed.

Installation and use

The Taro project is based on node. Please make sure that you have a newer node environment (>=12.0.0). It is recommended to use the node version management tool nvm to manage node, so that you can easily switch node versions and do not need to add sudo during global installation.

CLI Tool Installation

First, you need to install @tarojs/cli using npm or yarn globally, or use npx directly:

# use npm install CLI
$ npm install -g @tarojs/cli

# OR use yarn install CLI
$ yarn global add @tarojs/cli

# OR install cnpm,use cnpm install CLI
$ cnpm install -g @tarojs/cli

Taro version information can be found using npm info, where you can see the current version

npm info @tarojs/cli

Taro an open cross-end cross-frame solution插图

Project initialization

Use commands to create template projects:

$ taro init myApp

npm 5.2+ can also use npx to create template projects without global installation:

$ npx @tarojs/cli init myApp

Taro an open cross-end cross-frame solution插图1

After the project is created, Taro will install the dependencies required by the project by default. The installation tools are used to detect the dependencies in the yarn > cnpm > npm order. In general, the dependency installation will be relatively smooth, but in some cases the installation may fail, in which case you can use the installation command in the project directory to install yourself:

# Go to the project root directory
$ cd myApp

# use yarn Installation dependency
$ yarn

# OR use cnpm Installation dependency
$ cnpm install

# OR use npm Installation dependency
$ npm install

compilation run

Use Taro’s build command to compile Taro code into different side code, and then view the effect in the corresponding development tool.

Taro compilation is divided into dev and build modes:

  • dev mode (added –watch parameter) will listen for file modifications.
  • build mode (without the –watch parameter) will not listen for file modifications and will compress and package the code.
  • The files generated in dev mode are large. Setting the environment variable NODE_ENV to production can enable compression for easy preview, but the compilation speed will be reduced.

Example byte applet:

compile command

# yarn
$ yarn dev:tt
$ yarn build:tt

# npm script
$ npm run dev:tt
$ npm run build:tt

# Global installation only
$ taro build --type tt --watch
$ taro build --type tt

# npx It is also available to users
$ npx taro build --type tt --watch
$ npx taro build --type tt

# watch Simultaneous compression
$ set NODE_ENV=production && taro build --type tt --watch # Windows
$ NODE_ENV=production taro build --type tt --watch # Mac

Small program developer tools

Download and open the Bytedance applet Developer tool, and make sure the applet project configuration file project.tt.json is set. Then select the dist directory in the root of the project (the directory of the outputRoot setting in root config) to preview.

Note the project Settings of the developer tools:

  • The ES6 to ES5 function needs to be disabled. If this function is enabled, an error may be reported
  • You need to disable automatic style completion when uploading code. If enabled, an error may be reported
  • You need to disable code compression upload. If this function is enabled, an error may be reported

Project progression and optimization

CSS instrument

In Taro, we can use CSS preprocessor and post-processor freely, and the method of using CSS is very simple, as long as the relevant plug-ins are added in the compilation configuration:

const config = {
  projectName: 'v2ex',
  date: '2018-8-3',
  designWidth: 750,
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [
    '@tarojs/plugin-sass', // use Sass
    // '@tarojs/plugin-less', // use Less
    // '@tarojs/plugin-stylus', // use Stylus
  ],
  defineConstants: {
  },
  mini: {

  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    module: {
      postcss: {
        autoprefixer: {
          enable: true
        }
      }
    }
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}

Multiterminal development

In some cases, the performance or business logic of different platforms is qualitatively different. In this case, we can not do “one set of code to go around the world”.

For example, we are implementing the V2EX forum application, and the current API cannot be called cross-domain in the browser, so we need to use another API on the H5 side. We can solve this with built-in environment variables:

- import api from '../../utils/api'
// We can introduce different apis depending on the API
+ let api
+ if (process.env.TARO_ENV === 'weapp') {
+  api = require('../../utils/api-weapp')
+ } else if (process.env.TARO_ENV === 'h5') {
+  api = require('../../utils/api-h5')
+ }

Taro also provides a unified interface of multi-terminal files, through different naming ways to find dependencies, in such cases, we can keep:

import api from '../../utils/api'

Modify our file structure by adding the name of the platform between the file name and the suffix:

.
└── utils
    ├── api.h5.js
    ├── api.weapp.js
    └── index.js
资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code Taro an open cross-end cross-frame solution https://ictcoder.com/kyym/taro-an-open-cross-end-cross-frame-solution.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service