Home > Web Front-end > uni-app > A brief analysis of some common writing methods of UniApp

A brief analysis of some common writing methods of UniApp

PHPz
Release: 2023-04-20 15:16:03
Original
1204 people have browsed it

UniApp is a cross-platform development framework based on Vue.js, which can develop applications for multiple platforms such as iOS, Android, H5 and applets. In the development of UniApp, there are some more important writing methods that we need to pay attention to and master. Let's learn more about them below.

1. Framework structure

The source code of UniApp mainly consists of three parts: basic library, compiler and platform code. Among them, the basic library consists of two parts: uni-core and uni-helpers, which are responsible for providing necessary logical support for UniApp. The compiler mainly builds and packages the project, while the platform code provides corresponding APIs, component libraries and UI framework etc.

2. Directory structure

In the development of UniApp, we should give priority to the directory structure of the project. In UniApp, you can quickly create a UniApp project by clicking "HbuilderX -> New UniApp Project" and automatically add the directory structure, as follows:

├── App.vue
├── common
│ ├── api.js
│ ├── config.js
│ ├── mixin.js
│ └── utils.js
├── components
├── main.js
├── manifest.json
├── pages
│ └── index
│ │ ├── index.vue
│ └── main.js
├── pages.json
├── static
│ └── logo.png
└── uni.scss

Among them, App.vue is the entry file; The common directory is a public resource, including api.js (interface request), config.js (configuration information), mixin.js (mix), utils.js (tool class) and other files; components is a component library that can store some of your own packages components; main.js is the entry JS file, manifest.json is the project configuration file, pages is a collection of pages, including each sub-page and component, and pages.json is the page configuration file. The static folder is a static resource directory, which contains some pictures, styles, JS, etc.

3. Components

In UniApp, the use of components is very similar to that in Vue.js. You only need to use the template tag to define the component template. At the same time, UniApp also provides some commonly used component libraries, such as uni-icons (icons), uni-list (list), uni-form (form), uni-tabbar (bottom menu bar), etc., which can also be customized as needed. Component library.

4. Routing

In the development of UniApp, routing is also a very important part. You can configure routing in pages.json, as follows:

{

"pages": [
    {
        "path": "pages/index/index",
        "style": {
            "navigationBarTitleText": "首页"
        }
    },
    {
        "path": "pages/detail/detail",
        "style": {
            "navigationBarTitleText": "详情页"
        }
    }
]
Copy after login

}

Two routes are defined here, pointing to "pages/index/index" and "pages/detail/detail" page. When jumping, you can use APIs such as uni.navigateTo and uni.redirectTo to complete the page jump.

5. Global variables and methods

In the development of UniApp, we usually need to define some global variables and methods to facilitate our development. You can implement global calls by defining public variables and methods in App.vue and then mounting them through Vue.prototype.$xxx. As follows:

<script></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">export default {     onLoad() {         console.log(this.globalData) //获取全局变量     },     globalData: {         uname: 'admin',         age: 20     },     onShow() {     } } //挂载全局方法 Vue.prototype.$myfunc = function () {     console.log('This is a global function!') }</pre><div class="contentsignin">Copy after login</div></div> <p></script>

6. Development and debugging

When developing UniApp, we can use the HbuilderX IDE for development and debugging. After completing the code writing and saving, you can run and debug the project by clicking "Run -> Run to mobile phone or simulator". In addition, you can also enable debugging mode at runtime and debug in the developer tools.

Summary

The above are some common writing methods of UniApp. By mastering these writing methods, we can develop UniApp more efficiently and develop better applications.

The above is the detailed content of A brief analysis of some common writing methods of UniApp. For more information, please follow other related articles on the PHP Chinese website!

source:php.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