uniapp knowledge summary

Directory

1. What is UniApp?

2. What are the characteristics and advantages of UniApp?

3. What is the operating principle of UniApp?

4. What is the directory structure of UniApp?

5. How to realize cross-terminal development in UniApp?

6. How to develop pages in UniApp?

7. How to use third-party UI components in UniApp?

8. What is the data binding and event binding of UniApp?

9. How to implement data caching and persistence in UniApp?

10. How to debug and package release in UniApp?


1. What is UniApp?

UniApp is an open source cross-platform application development framework. It is based on the Vue.js ecosystem and can quickly develop multi-terminal applications, including iOS, Android, H5, applets, quick applications, desktop applications, etc. UniApp has high development efficiency, which can greatly reduce repetitive development work and enable developers to focus more on the realization of business logic. At the same time, UniApp also provides a series of components and plug-ins to facilitate developers to quickly create high-quality applications.

2. What are the characteristics and advantages of UniApp?

UniApp is a Vue.js-based cross-platform development framework launched by DCloud, which can be used to develop iOS, Android, H5, and various small programs (such as WeChat applets, Alipay applets, etc.).

Key features and benefits of UniApp include:

  1. Cross-platform development greatly reduces development costs and time. UniApp can realize the development of multiple platforms through a set of codes, and can quickly build various applications.

  2. Based on the Vue.js development model, it has powerful componentization and modularization capabilities, making development efficient, simple, and easy to maintain.

  3. UniApp provides a rich plug-in market, built-in a variety of commonly used third-party plug-ins, such as maps, push, sharing, payment, etc., which can quickly realize functions.

  4. With the advantages of Vue.js, it has an efficient, flexible and responsive data binding method, which can improve development efficiency and user experience.

  5. Good performance, strong rendering ability, fast loading speed, supports asynchronous update, and provides a variety of optimization methods (such as lazyload, etc.).

  6. It has good scalability, supports the expansion of native plug-ins, and can quickly customize development.

To sum up, UniApp is an excellent cross-platform development framework, which has great advantages in development efficiency, performance, and richness of functions, and is very suitable for developing various applications.

3. What is the operating principle of UniApp?

UniApp is a cross-platform framework based on Vue.js, which can simultaneously develop multi-terminal applications such as iOS, Android, and H5. Its operating principle is as follows:

  1. Build applications based on the Vue.js template engine and rendering principles.

  2. Through the encapsulated Native API, native functions are called to achieve cross-platform effects.

  3. Use the packaging tool to package the application code into a format that meets the requirements of each platform.

  4. At runtime, call the corresponding Native API to make the application run and display on different platforms.

In general, the operating principle of UniApp is to convert Vue code into native code, and achieve cross-platform effects by calling native API.

4. What is the directory structure of UniApp?

The directory structure of UniApp is as follows:

├── manifest.json // configuration file
├── pages // Folder for storing pages
│ ├── index
│ │ ├── index.vue
│ │ ├── main.js
│ │ └── ...
│ ├── otherPage
│ │ ├── index.vue
│ │ ├── main.js
│ │ └── ...
│ └──...
├── static // store static files
│ ├── image
│ ├── font
│ └──...
├── components // store components
│ ├── myComponent.vue
│ ├── otherComponent.vue
│ └──...
├── App.vue // Application component
└── main.js // application entry file

Among them, manifest.json is the configuration file of the application, pages folder stores pages, static folder stores static files, components The folder stores components, App.vue is the application component, and main.js is the entry file of the application.

5. How to implement cross-terminal development in UniApp?

UniApp is a cross-end development framework based on Vue.js, which can support application development on multiple platforms at the same time. Here are some ways to achieve cross-end development in UniApp:

  1. Use conditional compilation

UniApp supports conditional compilation, and different codes can be written according to the platform type, so that the same code can run on different platforms. Conditional compilation can be achieved using directives such as #ifdef, #ifndef, #endif, #define, etc.

For example:

#ifdef H5
  // H5 platform code
#endif

#ifdef APP-PLUS
  // APP platform code
#endif

#ifdef MP-WEIXIN
  // applet code
#endif
  1. Use cross-platform components

UniApp has some built-in cross-end components, such as uni-icons, uni-badge, etc., and the same components can be used on different platforms. At the same time, it also supports the use of third-party cross-platform component libraries, such as vant-weapp, iview-weapp, etc.

  1. Use platform APIs

UniApp supports calling native APIs of different platforms, which can be called by means of uni.xxx. For example:

uni.showModal({
  title: 'Prompt',
  content: 'This is a modal popup',
  success: function (res) {
    if (res. confirm) {
      console.log('User clicks OK');
    } else if (res. cancel) {
      console.log('User clicked cancel');
    }
  }
});

The above are some methods to achieve cross-end development in UniApp, I hope it can help you.

6. How to develop pages in UniApp?

UniApp is a cross-platform development tool based on the Vue.js framework, which can be used to develop mobile applications, applets and H5 pages. The following are the steps for page development in UniApp:

  1. Create a UniApp project. Open HBuilderX, click New Project, select the UniApp project type, fill in the project name and path, and click Create.

  2. Page componentization. Create a page file in the pages directory, and use Vue.js syntax to write the page code. Pages can be built using the uni-ui component library or custom components.

  3. Page routing configuration. Configure page routing information in the pages.json file, including page path, page name, page style, etc.

  4. Page debugging. You can use the debugging function of HBuilderX to view the page effect on the emulator or real machine. You can also use the WeChat developer tools or debugging tools provided by other platforms for debugging.

  5. Page optimization. You can use Vue.js optimization methods to improve page performance, such as asynchronous components, lazy loading of routes, code splitting, etc.

  6. Published online. Select Publish in HBuilderX to publish the application to various platforms, such as iOS, Android, WeChat Mini Program, Alipay Mini Program, etc.

The above are the basic steps for page development in UniApp, I hope it will be helpful to you.

7. How to use third-party UI components in UniApp?

The steps to use third-party UI components in UniApp are as follows:

  1. Download and install a third-party UI component library, which can be downloaded through npm or downloaded from the official website. Take the Vant component library as an example, enter npm install vant –save on the command line to install.

  2. Introduce the component library in the project, and introduce the component library that needs to be used in the app.vue file. In the Vant component library, you can introduce import Vant from ‘vant’ and import ‘vant/lib/index.css’ in app.vue to introduce component libraries and styles.

  3. Register components in pages or components that need to use UI components. In the Vant component library, the component library can be registered through Vue.use(Vant).

  4. Use components. In the Vant component library, you can directly use the component where you need to use it, such as .

The above are the basic steps for using third-party UI components in UniApp. Different third-party UI component libraries may be slightly different, but the basic process is similar.

8. What is the data binding and event binding of UniApp?

UniApp’s data binding and event binding are similar to other mainstream frameworks, and are implemented using a syntax similar to Vue.js.

Data binding:

In UniApp, you can use a two-way data binding syntax similar to Vue.js to bind data to elements on the page. The specific implementation is as follows:

  1. Declare the data variables that need to be bound in data
data: {
  message: 'Hello UniApp!'
}
  1. Use double bracket syntax ({{}}) to bind variables to elements on the page
<view>{<!-- -->{ message }}</view>
  1. When the value of the data variable changes, the page element is automatically updated
this. message = 'Hello World!'

Event binding:

In UniApp, you can use v-on syntax similar to Vue.js to bind events to elements on the page. The specific implementation is as follows:

  1. Declare the events that need to be bound and the corresponding handlers in the element
<button type="primary" @click="onClick">Click me</button>
  1. Define event handlers in methods
methods: {
  onClick() {
    console.log('Button clicked')
  }
}
  1. When the element is clicked, the event handler function will be called

9. How to implement data caching and persistence in UniApp?

UniApp can realize data caching and persistence by using local storage and caching technology. The specific implementation method can be selected from the following methods according to the specific requirements of the application and the size of the data volume and other factors:

  1. uni.setStorageSync() and uni.getStorageSync() methods: These two methods can store data in the local cache to achieve data caching and persistence. The specific code is implemented as follows:
// store data in local cache
uni.setStorageSync('key', 'value')

// fetch data from local cache
const data = uni. getStorageSync('key')
  1. uni.setStorage() and uni.getStorage() methods: These two methods can store data in local storage. Unlike local cache, local storage can store larger amounts of data. The specific code is implemented as follows:
// store data in local storage
uni.setStorage({
  key: 'key',
  data: 'value',
  success: function () {
    console.log('Data storage succeeded')
  }
})

// Get data from local storage
uni. getStorage({
  key: 'key',
  success: function (res) {
    const data = res.data
    console.log('Data obtained successfully', data)
  }
})
  1. Use third-party plug-ins: UniApp also provides some third-party data caching and persistence plug-ins, such as: uniCache, uni-storage, etc. Using these plug-ins can more easily achieve data caching and persistence.

Regardless of the method used, data security and legality must be paid attention to when implementing data caching and persistence. When storing sensitive data, you need to pay attention to security such as encryption and access control. At the same time, the stored data needs to be legal and cannot violate relevant laws, regulations and regulations.

10. How to debug and package release in UniApp?

UniApp provides tools for debugging and packaging and publishing, and you can use the following steps to operate:

  1. debugging

During the development process, you can use HBuilder X to debug UniApp. First of all, you need to choose a running platform in the UniApp project, such as WeChat applet, App or H5, etc. Then, click the “Run” button to run the application on a real device or emulator. In the developer tools, you can view debug output and error messages. If you need to debug on the web side, you can use the browser’s developer tools for debugging.

2. Package release

In HBuilder X, you can use the “Release” function to package and publish UniApp applications. In this function, you can select the platform and version number to be packaged, set the name, icon, and startup interface of the application, and then package it. After the packaging is completed, the installation package or code package of the application will be generated, which can be uploaded to the application store or server for distribution. At the same time, you can also use the cloud packaging function provided by HBuilder X for packaging and publishing, reducing the workload of developers.