CommonConstants.ets
/**
* Common constants for all features.
*/
export default class CommonConstants {
/**
* The index page message.
*/
static readonly INDEX_MESSAGE: string = 'Index Page';
/**
* The src msg.
*/
static readonly SECOND_SRC_MSG: string = 'Index页面传来的数据';
/**
* The second page url.
*/
static readonly SECOND_URL: string = 'pages/SecondPage';
/**
* The second page message.
*/
static readonly SECOND_MESSAGE: string = 'Second Page';
/**
* The src param.
*/
static readonly SECOND_SRC_PARAM: string = 'src';
/**
* Full the width.
*/
static readonly FULL_WIDTH: string = '100%';
/**
* Full the height.
*/
static readonly FULL_HEIGHT: string = '100%';
/**
* The hello world font size.
*/
static readonly FONT_SIZE: string = '38fp';
/**
* The param font size.
*/
static readonly PARAMS_FONT_SIZE: string = '20fp';
/**
* The button font size.
*/
static readonly BUTTON_FONT_SIZE: string = '16fp';
/**
* The button width.
*/
static readonly BUTTON_WIDTH: string = '296vp';
/**
* The button height.
*/
static readonly BUTTON_HEIGHT: string = '40vp';
/**
* The layout height.
*/
static readonly LAYOUT_HEIGHT: string = '140vp';
/**
* The param opacity.
*/
static readonly PARAMS_OPACITY: number = 0.6;
}
Logger.ets
import { hilog } from '@kit.PerformanceAnalysisKit';
/**
* Common log for all features.
*/
export class Logger {
private domain: number;
private prefix: string;
private format: string = `%{public}s, %{public}s`;
constructor(prefix: string) {
this.prefix = prefix;
this.domain = 0xFF00;
}
debug(...args: string[]) {
hilog.debug(this.domain, this.prefix, this.format, args);
}
info(...args: string[]) {
hilog.info(this.domain, this.prefix, this.format, args);
}
warn(...args: string[]) {
hilog.warn(this.domain, this.prefix, this.format, args);
}
error(...args: string[]) {
hilog.error(this.domain, this.prefix, this.format, args);
}
fatal(...args: string[]) {
hilog.fatal(this.domain, this.prefix, this.format, args);
}
isLoggable(level: number) {
hilog.isLoggable(this.domain, this.prefix, level);
}
}
export default new Logger('[PageRouter]');
IndexPage.ets
import { router } from '@kit.ArkUI';
import CommonConstants from '../common/constants/CommonConstants';
import Logger from '../common/utils/Logger';
const TAG = '[IndexPage]';
class routerParams {
src:string
constructor(str:string) {
this.src = str
}
}
/**
* The IndexPage is the entry point of the application.
*/
@Entry
@Component
struct IndexPage {
@State message: string = CommonConstants.INDEX_MESSAGE;
build() {
Row() {
Column() {
Text(this.message)
.fontSize(CommonConstants.FONT_SIZE)
.fontWeight(FontWeight.Bold)
Blank()
Button($r('app.string.next'))
.fontSize(CommonConstants.BUTTON_FONT_SIZE)
.width(CommonConstants.BUTTON_WIDTH)
.height(CommonConstants.BUTTON_HEIGHT)
.backgroundColor($r('app.color.button_bg'))
.onClick(() => {
router.pushUrl({
url: CommonConstants.SECOND_URL,
params: new routerParams(CommonConstants.SECOND_SRC_MSG)
}).catch((error:Error) => {
Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));
});
})
}
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.LAYOUT_HEIGHT)
}
.height(CommonConstants.FULL_HEIGHT)
.backgroundColor($r('app.color.page_bg'))
}
}
SecondPage.ets
import { router } from '@kit.ArkUI';
import CommonConstants from '../common/constants/CommonConstants';
class routerParams {
src:string = ''
}
/**
* The SecondPage is the router push page.
*/
@Entry
@Component
struct SecondPage {
@State message: string = CommonConstants.SECOND_MESSAGE;
@State params: routerParams = router.getParams() as routerParams
@State src: string = this.params.src;
build() {
Row() {
Column() {
Text(this.message)
.fontSize(CommonConstants.FONT_SIZE)
.fontWeight(FontWeight.Bold)
Text(this.src)
.fontSize(CommonConstants.PARAMS_FONT_SIZE)
.opacity(CommonConstants.PARAMS_OPACITY)
Blank()
Button($r('app.string.back'))
.fontSize(CommonConstants.BUTTON_FONT_SIZE)
.width(CommonConstants.BUTTON_WIDTH)
.height(CommonConstants.BUTTON_HEIGHT)
.backgroundColor($r('app.color.button_bg'))
.onClick(() => {
router.back();
})
}
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.LAYOUT_HEIGHT)
}
.height(CommonConstants.FULL_HEIGHT)
.backgroundColor($r('app.color.page_bg'))
}
}
代码结构
效果截图