Home > Web Front-end > uni-app > body text

Let's talk about how beginners use uniapp

PHPz
Release: 2023-04-10 15:10:18
Original
877 people have browsed it

As the mobile application market grows, more and more companies or individuals begin to develop their own mobile applications. However, many developers are deterred by problems such as learning multiple programming languages, adapting to various mobile phone systems and screens of different sizes. The Swiss army knife-like cross-platform development framework uniapp came into being. It is a development framework that can run on multiple platforms, allowing developers to develop full-platform applications that are applicable to multiple platforms under one code.

Uniapp is based on the Vue framework and compiles its source code into native applications for different platforms. Supported platforms include: iOS, Android, H5, mini programs, quick apps, apps, etc. This article will guide beginners on how to use uniapp.

Install uniapp

First of all, before installing uniapp, you need to install the Node.js environment and ensure that its version is 8.0 or above.

After installing the Node.js environment, we can use the npm (Node.js package management tool) command line to install the uniapp scaffolding tool.

npm install -g @vue/cli
vue create -p dcloudio/uni-preset-vue my-project
Copy after login

In this way, you can successfully install uniapp and create an initial project my-project.

Directory structure of uniapp

After creating the project, let’s take a look at the directory structure of the project. The main folders of the project include:

  • components: storage component folder, ending with .vue suffix.
  • pages: Storage page folder, ending with .vue suffix.
  • static: Store static files, such as pictures and fonts, etc.
  • unpackage: The folder generated after the project is packaged.
  • App.vue: The main page of the entire application.
  • main.js: The entry file for the entire application.

Writing uniapp application

We have successfully installed uniapp and created an initial project, so next, let's write some simple code.

Create a page

Create a new vue file in the pages folder of the project, such as home.vue. Write the following code in the file:

<template>
  <view class="content">
    <text>{{ greeting }}</text>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        greeting: 'Hello uniapp!'
      }
    }
  }
</script>
Copy after login

In this way, we create a new page.

Navigate to another page

Next step, let’s say we want to click a button in page 1 and jump to page 2. We can add the following code to page 1:

<button @click="goToPage2">Go to page 2</button>
Copy after login

After clicking the button, we need to add the following code to the script tag of page 1:

methods: {
  goToPage2() {
    uni.navigateTo({
      url: '/pages/page2.vue'
    })
  }
}
Copy after login

In this way, we can add the following code to page 1 Jump to page 2.

Introduction of pictures

Suppose we need to introduce a picture into the page. You need to store the picture in the static folder first, and then use the relative path to reference it in the .vue file.

<template>
  <view>
    <image :src="`/static/${image}`" mode="aspectFit"></image>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        image: 'image.jpg'
      }
    }
  }
</script>
Copy after login

Initiate a network request

get request:

uni.request({
  url: 'https://www.xxx.com/api/list',
  data: {
    page: 1,
    limit: 10
  },
  success(res) {
    console.log(res)
  }
})
Copy after login

post request:

uni.request({
  url: 'https://www.xxx.com/api/list',
  method: 'POST',
  data: {
    page: 1,
    limit: 10
  },
  success(res) {
    console.log(res)
  }
})
Copy after login

Run uniapp

After completing the above code writing, We need to run the following command in the command line:

npm run dev:%PLATFORM%
Copy after login

%PLATFORM% represents the required platform, you can fill in iOS, Android, etc. here. After running the above command, you can preview the project effect on the simulator or real machine.

Summary

uniapp provides developers with cross-platform development solutions, greatly reducing development costs and improving development efficiency. Using it, developers only need to master one programming language to develop full-platform applications on multiple platforms, helping developers conduct development operations more efficiently.

The above is the detailed content of Let's talk about how beginners use 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!