路由切换
import router from '@ohos.router'
@Entry
@Component
struct Index {
@State message: string = 'Hello World 123466788'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Next')
.fontSize(30)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('40%')
.height('5%')
.onClick(() => {
console.info(`Succeeded in clicking the 'Next' button.`)
router.pushUrl({ url: 'pages/Second' }).then(() => {
console.info('Succeeded in jumping to the second page.')
console.log("123456789")
}).catch((err) => {
console.log("123456789")
})
})
}
.width('100%')
}
.height('100%')
}
}
import router from '@ohos.router'
@Entry
@Component
struct Second {
@State message: string = 'Hi there'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Back')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('5%')
.onClick(() => {
console.info(`Succeeded in clicking the 'Back' button.`)
try {
router.back()
console.info('Succeeded in returning to the first page.')
} catch (err) {
console.log(err,'this is error')
}
})
}
.width('100%')
}
.height('100%')
}
}
组件的使用
import router from '@ohos.router'
import { otherView } from '../view/otherView'
@Entry
@Component
struct Index {
@State message: string = 'Hello World 123466788'
build() {
Row() {
Column() {
otherView()
}
.width('100%')
}
.height('100%')
}
}
@Component
export struct otherView {
otherHeight:number = 150;
build() {
Column() {
Text("对未来的真正慷慨,是把一切都献给现在。")
.width('100%')
.fontColor(Color.White)
.padding({ top: 18, bottom: 10 })
Text("IP 归属:北京 >")
.width('100%')
.fontColor('#C2BFC7')
.padding({ bottom: 10 })
Text("+添加年龄、学校标签")
.fontColor(Color.White)
.fontSize(14)
.backgroundColor('#555A596A')
.borderRadius(5)
.padding(3)
Row({ space: 10 }) {
Button('申请认证', { type: ButtonType.Normal })
.borderRadius(5)
.fontWeight(FontWeight.Bold)
.backgroundColor('#555A596A')
.height(30)
.width(90)
.onClick(() => {
})
Button('编辑资料', { type: ButtonType.Normal })
.borderRadius(5)
.fontWeight(FontWeight.Bold)
.backgroundColor('#555A596A')
.height(30)
.width(90)
.onClick(() => {
})
Button({ type: ButtonType.Normal }) {
}
.backgroundColor('#555A596A')
.borderRadius(5)
.height(30)
.width(30)
}
.width('100%')
.padding({ bottom: 18, top: 10 })
}
.height(this.otherHeight)
.alignItems(HorizontalAlign.Start)
.padding({ left: 15 })
.backgroundColor(0x282350)
.width('100%')
}
}