鸿蒙开发总结(三):导航与路由机制

在鸿蒙应用开发中,导航和路由机制是实现应用内部页面跳转和参数传递的关键技术。本文将探讨这些技术点,并提供面试中可能遇到的问题和答案。

Navigation 与 Router

Navigation 和 Router 都是用于管理应用导航的工具,但它们的关注点和适用场景有所不同。

Navigation

关注页面之间的跳转和用户交互,如从主页跳转到详情页。通过@ohos.router模块的router.pushstartAbility实现页面跳转和参数传递。

以下是使用router.push进行页面跳转并传递参数的示例代码:

typescript

import router from '@ohos.router';

// 在当前页面的某个事件处理函数中触发跳转
const handleNavigate = () => {
    const params = {
        id: 1,
        name: 'example'
    };
    router.push({
        url: 'pages/DetailPage',
        params: params
    });
};

在目标页面DetailPage中接收参数:

typescript

import router from '@ohos.router';

@Entry
@Component
struct DetailPage {
    private receivedParams: any;

    onPageShow() {
        this.receivedParams = router.getParams();
        console.log('Received params:', this.receivedParams);
    }

    build() {
        // 页面UI构建
    }
}

Router

关注 URL 与页面之间的映射关系,如根据 URL 加载不同的页面。通过@ohos.router模块实现,支持路由配置、参数解析和重定向等功能。

以下是路由配置及使用的示例代码:

typescript

import router from '@ohos.router';

// 路由配置
router.addRoutes([
    {
        path: '/home',
        component: 'pages/HomePage'
    },
    {
        path: '/detail/:id',
        component: 'pages/DetailPage'
    }
]);

// 根据URL进行导航
router.push({
    url: '/detail/2'
});

DetailPage中解析参数:

typescript

import router from '@ohos.router';

@Entry
@Component
struct DetailPage {
    private id: number;

    onPageShow() {
        const routeParams = router.getParams();
        this.id = routeParams.id;
        console.log('Received id:', this.id);
    }

    build() {
        // 页面UI构建
    }
}

导航和路由机制的选择对于应用的用户体验和维护性有着重要影响。面试中可能会讨论不同导航和路由机制的特点和适用场景。

易用性与功能层面

在易用性和功能层面,Navigation 和 Router 各有特点。

易用性层面

Navigation 天然具备标题、内容、回退按钮的功能联动,开发者可以直接使用此能力。Router 若要实现此能力,需要自行定义。

功能层面

Navigation 天然支持一多,Router 不支持。Navigation 没有路由数量限制,Router 限制 32 个。Navigation 可以嵌套在模态对话框中,Router 不支持。

理解 Navigation 和 Router 的不同特点和功能对于开发者来说至关重要。面试时可能会探讨如何选择合适的导航和路由机制以及如何在实际开发中实现页面跳转和参数传递。

Stage 模型与其他模型

鸿蒙中的 Stage 模型是一个面向对象的应用开发框架,它提供了组件化开发机制,支持多窗口、多设备的分布式场景。

Stage 模型

基于模块化设计,支持多个 Ability,更适合复杂应用。

以下是一个简单的 Stage 模型应用示例,包含两个 Ability:

java

// MainAbility.java
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_layout);

        Button jumpButton = (Button) findComponentById(ResourceTable.Id_jump_button);
        jumpButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent newIntent = new Intent();
                newIntent.setAction("action.second.ability");
                startAbility(newIntent);
            }
        });
    }
}

// SecondAbility.java
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;

public class SecondAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        new ToastDialog(this)
              .setText("Second Ability started")
              .show();
    }
}

FA 模型

基于单一 Ability,更适合简单应用。

以下是一个简单的 FA 模型应用示例:

java

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        new ToastDialog(this)
              .setText("FA Model App Started")
              .show();
    }
}

Stage 模型和其他模型的选择对于应用的架构和可维护性有着重要影响。面试中可能会讨论不同模型的特点和适用场景。

通过深入理解这些技术点,开发者可以更好地掌握鸿蒙开发的核心机制,构建出更加健壮和高效的应用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值