在这里插入图片描述

  1. 前端(使用React Native):bjgdnet.cn
    React Native是一个用于构建跨平台移动应用的框架。

jsx
// App.js (React Native)
import React from ‘react’;
import { View, Text, Button } from ‘react-native’;

const App = () => {
const onPress = () => {
alert(‘Hello, World!’);
};

return (

Welcome to My App


);
};

export default App;
2. 后端(使用Node.js和Express):
Node.js和Express常用于构建服务器端应用。

javascript
// app.js (Node.js with Express)
const express = require(‘express’);
const app = express();
const port = 3000;

app.get(‘/’, (req, res) => {
res.send(‘Hello, World!’);
});

app.listen(port, () => {
console.log(Server is running on port ${port});
});
3. 数据库(使用MongoDB和Mongoose):
Mongoose是一个在Node.js环境中流行的MongoDB对象模型工具。

javascript
// models/User.js (Mongoose)
const mongoose = require(‘mongoose’);
const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: String,
email: String,
});

module.exports = mongoose.model(‘User’, UserSchema);
4. 安卓原生(使用Java):
虽然React Native可以构建跨平台的移动应用,但你也可以使用原生语言(如Java或Kotlin)来构建安卓应用。

java
// MainActivity.java (Android)
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    findViewById(R.id.my_button).setOnClickListener(v -> {  
        Toast.makeText(this, "Hello, World!", Toast.LENGTH_SHORT).show();  
    });  
}  

}
5. iOS原生(使用Swift):
对于iOS应用,你可以使用Swift或Objective-C。

swift
// ViewController.swift (iOS)
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {  
    super.viewDidLoad()  
    // Do any additional setup after loading the view.  
      
    let button = UIButton(type: .system)  
    button.setTitle("Press Me", for: .normal)  
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)  
    button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)  
    view.addSubview(button)  
}  

@objc func buttonPressed() {  
    let alert = UIAlertController(title: "Hello", message: "World!", preferredStyle: .alert)  
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))  
    self.present(alert, animated: true, completion: nil)  
}  

}
这些代码片段只是开始,完整的App开发需要更多的代码和配置。由于一个完整的App代码通常涉及多个部分(如前端、后端、数据库等),并且不同语言用于不同的目的,我将为你提供使用几种常见语言编写的简单示例代码片段,这些代码片段可以用于构建App的不同部分。

  1. 前端(使用React Native):
    React Native是一个用于构建跨平台移动应用的框架。

jsx
// App.js (React Native)
import React from ‘react’;
import { View, Text, Button } from ‘react-native’;

const App = () => {
const onPress = () => {
alert(‘Hello, World!’);
};

return (

Welcome to My App


);
};

export default App;
2. 后端(使用Node.js和Express):
Node.js和Express常用于构建服务器端应用。

javascript
// app.js (Node.js with Express)
const express = require(‘express’);
const app = express();
const port = 3000;

app.get(‘/’, (req, res) => {
res.send(‘Hello, World!’);
});

app.listen(port, () => {
console.log(Server is running on port ${port});
});
3. 数据库(使用MongoDB和Mongoose):
Mongoose是一个在Node.js环境中流行的MongoDB对象模型工具。

javascript
// models/User.js (Mongoose)
const mongoose = require(‘mongoose’);
const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: String,
email: String,
});

module.exports = mongoose.model(‘User’, UserSchema);
4. 安卓原生(使用Java):
虽然React Native可以构建跨平台的移动应用,但你也可以使用原生语言(如Java或Kotlin)来构建安卓应用。

java
// MainActivity.java (Android)
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    findViewById(R.id.my_button).setOnClickListener(v -> {  
        Toast.makeText(this, "Hello, World!", Toast.LENGTH_SHORT).show();  
    });  
}  

}
5. iOS原生(使用Swift):
对于iOS应用,你可以使用Swift或Objective-C。

swift
// ViewController.swift (iOS)
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {  
    super.viewDidLoad()  
    // Do any additional setup after loading the view.  
      
    let button = UIButton(type: .system)  
    button.setTitle("Press Me", for: .normal)  
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)  
    button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)  
    view.addSubview(button)  
}  

@objc func buttonPressed() {  
    let alert = UIAlertController(title: "Hello", message: "World!", preferredStyle: .alert)  
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))  
    self.present(alert, animated: true, completion: nil)  
}  

}
这些代码片段只是开始,完整的App开发需要更多的代码和配置。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐