Ionic React 快速入门简介
什么是 Ionic 框架?
¥What is Ionic Framework?
首先,如果你是新来的,欢迎!Ionic 是一个免费的开源组件库,用于构建在 iOS、Android、Electron 和 Web 上运行的应用。你可以使用熟悉的技术(HTML、CSS、JavaScript)编写一次应用并部署到任何平台。
¥First off, if you're new here, welcome! Ionic is a free and open source component library for building apps that run on iOS, Android, Electron, and the Web. You write your app once using familiar technologies (HTML, CSS, JavaScript) and deploy to any platform.
除了 UI 组件之外,Ionic 还提供了一个命令行工具,用于创建新应用以及部署到我们支持的各种平台。
¥Along with the UI components, Ionic also provides a command line tool for creating new apps, as well as deploying to the various platforms we support.
在本指南中,我们将介绍 React 和 Ionic 的基础知识,包括任何 Ionic 特定功能。如果你熟悉 React,请享受本指南并了解有关 Ionic 的新知识。如果你对其中任何一个都不熟悉,不用担心!本指南将涵盖基础知识并提供足够的信息来启动和运行应用。
¥In this guide, we'll go over the basics of both React and Ionic, including any Ionic specific features. If you're familiar with React, enjoy the guide and learn something new about Ionic. If you're not familiar with either, no worries! This guide will cover the basics and provide enough information to get an app up and running.
使用 Ionic CLI 创建项目
¥Creating a project with the Ionic CLI
首先,让我们安装最新版本的 Ionic CLI。
¥To get started, let's install the latest version of the Ionic CLI.
npm install -g @ionic/cli
从这里开始,全局命令 ionic
将允许使用 Ionic 和任何其他依赖创建 React 项目。要创建新项目,请运行以下命令:
¥From here, the global command ionic
will allow for the creation of a React project with Ionic and any other dependencies. To create a new project, run the following command:
ionic start myApp blank --type=react
cd myApp
从这里,我们运行 ionic serve
并让我们的项目在浏览器中运行。
¥From here, we run ionic serve
and have our project running in the browser.
看看 React 组件
¥A look at a React Component
我们应用的基础将位于 src
目录中,主入口点将是我们的 index.tsx
。如果我们在代码编辑器中打开项目并打开 src/index.tsx
,我们应该看到以下内容:
¥The base of our app will be in the src
directory, and the main entry point will be our index.tsx
. If we open our project in a code editor and open src/index.tsx
, we should see the following:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
那么这是怎么回事呢?嗯,前三行引入了一些依赖。第一个是 React 本身。这使我们能够使用类似 HTML 的语法(称为 JSX)编写组件。稍后我们将讨论 JSX。
¥So what's going on here? Well, the first three lines are pulling in some dependencies. The first being React itself. This allows us to write components in an HTML-like syntax called JSX. We'll talk about JSX a bit later on.
第二个导入是针对 ReactDOM 的。ReactDOM.render
方法是浏览器/DOM 特定的方法,用于获取我们的组件并将其渲染到指定的 DOM 节点。
¥The second import is for ReactDOM. The ReactDOM.render
method is the browser/DOM specific way of taking our components and rendering it to a specified DOM node.
最后导入的是我们应用的根组件,简单地命名为 App
。这是我们的第一个 React 组件,将在 React 应用的引导过程中使用。
¥The last import is the root component for our app, simply named App
. This is our first React component and will be used in the bootstrapping process for our React app.
如果我们打开 App.tsx
,我们应该看到以下内容。
¥If we open App.tsx
, we should see the following.
import React from 'react';
import { Route } from 'react-router-dom';
import { IonApp, IonRouterOutlet } from '@ionic/react';
import { IonReactRouter } from '@ionic/react-router';
import Home from './pages/Home';
/* Core CSS required for Ionic components to work properly */
import '@ionic/react/css/core.css';
const App: React.FC = () => (
<IonApp>
<IonReactRouter>
<IonRouterOutlet>
<Route path="/home" component={Home} exact={true} />
<Route exact path="/" render={() => <Redirect to="/home" />} />
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
);
乍一看,似乎发生了很多事情,所以让我们从第一组导入开始进行分解。
¥At first glance, it may look like a lot is going on, so let's break it down, starting with the first group of imports.
import React from 'react';
import { Route } from 'react-router-dom';
import { IonApp, IonRouterOutlet } from '@ionic/react';
import { IonReactRouter } from '@ionic/react-router';
import Home from './pages/Home';
与 index.tsx
类似,我们首先必须导入 React 才能使用 JSX。
¥Similar to index.tsx
, we first must import React to use JSX.
下一次导入来自 react-router-dom
。我们正在导入 Route,这就是我们将应用的 URL 与我们想要渲染的组件相匹配的方式
¥The next import is from react-router-dom
. We're importing Route, which is how we’ll match the app’s URL with the components we want to render
继 ReactRouter 之后,我们接下来首次导入 Ionic。要在 React 中使用组件,必须首先导入它。因此,对于 Ionic 来说,这意味着任何时候我们想要使用 Button 或 Card 时,都必须将其添加到我们的导入中。对于我们的应用组件,我们仅使用 IonApp
、IonRouterOutlet
和 IonReactRouter
。
¥Following ReactRouter, we next have our first imports for Ionic. To use a component in React, you must first import it. So for Ionic, this means anytime we want to use a Button or a Card, it must be added to our imports. In the case of our App component, we're only using IonApp
, IonRouterOutlet
, and IonReactRouter
.
IonReactRouter
是一个封装 ReactRouter 的 BrowserRouter 组件的组件。它的行为或多或少与 BrowserRouter 相同,但有一些差异。我们有一个更深入的指南来讨论 React 导航文档 中的这些差异。
¥IonReactRouter
is a component that wraps ReactRouter’s BrowserRouter component. It more or less behaves the same as BrowserRouter with a few differences. We have a deeper guide that goes over these differences in our React Navigation Docs.
最后一个重要的导入是 Home
组件导入。这是我们能够在应用中导航到的组件。稍后我们将讨论导航部分。
¥The last important import is the Home
component import. This is a component that we will be able to navigate to in our app. We'll look at the navigation part a bit later.
CSS 导入从 Ionic 中引入实用样式,例如填充、排版等。
¥The CSS import is pulling in the utility styles from Ionic for things like padding, typography, etc.
在检查了所有导入之后,我们现在首先看一下 React 组件:
¥After reviewing all of the imports, we now get to our first look at a React Component:
const App: React.FC = () => (
<IonApp>
<IonReactRouter>
<IonRouterOutlet>
<Route path="/home" component={Home} exact={true} />
<Route exact path="/" render={() => <Redirect to="/home" />} />
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
);
该 React 组件为我们的应用设置初始路由,并包含一些用于动画和布局的核心 Ionic 组件(IonRouterOutlet 和 IonApp)。值得注意的一件事是,在 React 中,为了进行数据绑定,值在大括号 ({}
) 中传递。因此,在 Route
组件中,我们可以将 component
的值设置 为之前的 Home
组件。这就是 React 知道该值不是字符串,而是对组件的引用的方式。
¥This React component sets up the initial routing for our app, as well as include some core Ionic components for animations and layout (IonRouterOutlet and IonApp). One thing that stands out is that in React, to do data-binding, the value is passed in curly braces ({}
). So in the Route
component, we can set the value of component
to the Home
component from earlier. This is how React will know that that value is not a string, but a reference to a component.
这里需要注意的是,这些都是标准的 React DOM 库,这意味着没有自定义集成层或转译步骤。
¥What's important to note here is that these are all standard React DOM libraries, meaning there's no custom integration layer or transpilation step.
使用样式的组件
¥A component with style