当前位置: 首页 > news >正文

建设网站的知识唯尚广告联盟

建设网站的知识,唯尚广告联盟,自己做商品网站怎么做,计算机最吃香的专业以及工资目录: 1、发起网络请求的两种方式第一种使用httpRequest发送http的请求:1.1、在进行网络请求前,您需要在module.json5文件中申明网络访问权限1.2、GET 请求1.3、POST请求1.4、处理响应的结果第二种使用axios发送http的请求:1.1、在…

目录:

    • 1、发起网络请求的两种方式
      • 第一种使用httpRequest发送http的请求:
      • 1.1、在进行网络请求前,您需要在module.json5文件中申明网络访问权限
      • 1.2、GET 请求
      • 1.3、POST请求
      • 1.4、处理响应的结果
      • 第二种使用axios发送http的请求:
      • 1.1、在进行网络请求前,您需要在module.json5文件中申明网络访问权限
      • 1.2、安装axios
      • 1.3、使用axios发送请求
    • 2、异步的调用
    • 3、Promise.all()的用法
    • 4、鸿蒙一次开发,多端部署(自适应布局)
      • 4.1、拉伸能力
      • 4.2、均分能力
      • 4.3、占比能力
      • 4.4、缩放能力
      • 4.5、延伸能力
      • 4.6、隐藏能力
      • 4.7、折行能力
      • 4.8、应用实例
    • 5、鸿蒙一次开发,多端部署(响应式布局)
      • 5.1、断点
      • 5.2、媒资查询
      • 5.3、栅格布局
        • 5.3.1、缩进布局
        • 5.3.2、挪移布局
        • 5.3.3、重复布局

1、发起网络请求的两种方式

第一种使用httpRequest发送http的请求:

1.1、在进行网络请求前,您需要在module.json5文件中申明网络访问权限

{"module" : {"requestPermissions":[{"name": "ohos.permission.INTERNET"}]}
}

1.2、GET 请求

//导入http模块
import http from '@ohos.net.http';
//创建httpRequest对象
let httpRequest = http.createHttp();
//订阅请求头(可选)
httpRequest.on('headersReceive', (header) => {console.info('header: ' + JSON.stringify(header));
});//发送请求
let url= "https://EXAMPLE_URL?param1=v1&param2=v2";
let promise = httpRequest.request(// 请求url地址url,{// 请求方式method: http.RequestMethod.GET,// 可选,默认为60sconnectTimeout: 60000,// 可选,默认为60sreadTimeout: 60000,// 开发者根据自身业务需要添加header字段header: {'Content-Type': 'application/json'}});

1.3、POST请求

//导入http模块
import http from '@ohos.net.http';
//创建httpRequest对象
let httpRequest = http.createHttp();
//订阅请求头(可选)
httpRequest.on('headersReceive', (header) => {console.info('header: ' + JSON.stringify(header));
});//发送请求
let url = "https://EXAMPLE_URL";
let promise = httpRequest.request(// 请求url地址url,{// 请求方式method: http.RequestMethod.POST,// 请求的额外数据。extraData: {"param1": "value1","param2": "value2",},// 可选,默认为60sconnectTimeout: 60000,// 可选,默认为60sreadTimeout: 60000,// 开发者根据自身业务需要添加header字段header: {'Content-Type': 'application/json'}});

1.4、处理响应的结果

promise.then((data) => { if (data.responseCode === http.ResponseCode.OK) {console.info('Result:' + data.result);console.info('code:' + data.responseCode);}
}).catch((err) => {console.info('error:' + JSON.stringify(err));
});

第二种使用axios发送http的请求:

1.1、在进行网络请求前,您需要在module.json5文件中申明网络访问权限

{"module" : {"requestPermissions":[{"name": "ohos.permission.INTERNET"}]}
}

1.2、安装axios

ohpm install @ohos/axios

在这里插入图片描述

1.3、使用axios发送请求

import axios from "@ohos/axios";
@Entry
@Component
struct Dom {aboutToAppear() {axios.get("http://localhost/books",).then(res =>{let data:string = JSON.stringify(res);console.log(data);}).catch(err=> {console.log("请求失败");})}build() {Column({space: 30}) {}.width('100%').height('100%')}
}

2、异步的调用

// 假设你有一个网络请求的函数,例如使用fetch API
function fetchData(url) {return new Promise((resolve, reject) => {fetch(url).then(response => {if (response.ok) {response.json().then(data => resolve(data)).catch(error => reject(error));} else {reject(new Error('Network response was not ok.'));}}).catch(error => reject(error));});
}// 使用Promise进行异步请求
fetchData('https://your-api.com/data').then(data => {// 处理响应数据console.log(data);
}).catch(error => {// 处理错误console.error(error);
});

3、Promise.all()的用法

在这里插入图片描述

这里示例this.getWeather返回一个promise后,然后被push到数组promises中,Promise.all()方法处理数组promises,一次处理多个promise函数,后端返回的数据,前端也要通过相同的数据结构去接收。

4、鸿蒙一次开发,多端部署(自适应布局)

4.1、拉伸能力

在这里插入图片描述

4.2、均分能力

在这里插入图片描述

4.3、占比能力

在这里插入图片描述

4.4、缩放能力

在这里插入图片描述

4.5、延伸能力

在这里插入图片描述

4.6、隐藏能力

在这里插入图片描述

4.7、折行能力

在这里插入图片描述

4.8、应用实例

在这里插入图片描述

5、鸿蒙一次开发,多端部署(响应式布局)

当前系统提供了如下三种响应式布局能力,后文中我们将依次展开介绍:
在这里插入图片描述

5.1、断点

在这里插入图片描述

通过窗口对象监听断点变化的核心是获取窗口对象及注册窗口尺寸变化的回调函数:

// MainAbility.ts
import window from '@ohos.window'
import display from '@ohos.display'
import UIAbility from '@ohos.app.ability.UIAbility'export default class MainAbility extends UIAbility {private windowObj?: window.Windowprivate curBp: string = ''//...// 根据当前窗口尺寸更新断点private updateBreakpoint(windowWidth: number) :void{// 将长度的单位由px换算为vp//display.getDefaultDisplaySync().densityPixels是用来获取默认显示设备的屏幕密度的,单位是每英寸像素数(DPI)let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixelslet newBp: string = ''if (windowWidthVp < 320) {newBp = 'xs'} else if (windowWidthVp < 600) {newBp = 'sm'} else if (windowWidthVp < 840) {newBp = 'md'} else {newBp = 'lg'}if (this.curBp !== newBp) {this.curBp = newBp// 使用状态变量记录当前断点值AppStorage.setOrCreate('currentBreakpoint', this.curBp)}}onWindowStageCreate(windowStage: window.WindowStage) :void{windowStage.getMainWindow().then((windowObj) => {this.windowObj = windowObj// 获取应用启动时的窗口尺寸this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width)// 注册回调函数,监听窗口尺寸变化windowObj.on('windowSizeChange', (windowSize)=>{this.updateBreakpoint(windowSize.width)})});// ...}//...
}

在页面中,获取及使用当前的断点:

@Entry
@Component
struct Index {@StorageProp('currentBreakpoint') curBp: string = 'sm'build() {Flex({justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center}) {Text(this.curBp).fontSize(50).fontWeight(FontWeight.Medium)}.width('100%').height('100%')}
}

运行的效果:
在这里插入图片描述

5.2、媒资查询

在这里插入图片描述

import { BreakpointSystem } from '../util/BreakpointSystem'
import { BreakPointType } from '../util/BreakPointType'@Entry
@Component
struct Index {@StorageLink('currentBreakpoint') private currentBreakpoint: string = "md";@State private icon: Resource = $r('app.media.icon')private breakpointSystem: BreakpointSystem = new BreakpointSystem()aboutToAppear() {this.breakpointSystem.register()}aboutToDisappear() {this.breakpointSystem.unregister()}build() {Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {Image(new BreakPointType({sm:$r("app.media.biocn01"), md:$r("app.media.biocn02"), lg:$r("app.media.biocn03")}).getValue(this.currentBreakpoint)!).height(100).width(100).objectFit(ImageFit.Contain)Text(this.currentBreakpoint).fontSize(24).margin(10)}.width('100%').height('100%')}
}
// common/breakpointsystem.ets
import mediaQuery from '@ohos.mediaquery'interface Breakpoint {name: stringsize: numbermediaQueryListener?: mediaQuery.MediaQueryListener
}export class BreakpointSystem {private currentBreakpoint: string = 'md'private breakpoints: Breakpoint[] = [{ name: 'xs', size: 0 }, { name: 'sm', size: 320 },{ name: 'md', size: 600 }, { name: 'lg', size: 840 }]private updateCurrentBreakpoint(breakpoint: string) {if (this.currentBreakpoint !== breakpoint) {this.currentBreakpoint = breakpointAppStorage.Set<string>('currentBreakpoint', this.currentBreakpoint)console.log('on current breakpoint: ' + this.currentBreakpoint)}}public register() {this.breakpoints.forEach((breakpoint: Breakpoint, index) => {let condition:stringif (index === this.breakpoints.length - 1) {condition = '(' + breakpoint.size + 'vp<=width' + ')'} else {condition = '(' + breakpoint.size + 'vp<=width<' + this.breakpoints[index + 1].size + 'vp)'}console.log(condition)breakpoint.mediaQueryListener = mediaQuery.matchMediaSync(condition)breakpoint.mediaQueryListener.on('change', (mediaQueryResult) => {if (mediaQueryResult.matches) {this.updateCurrentBreakpoint(breakpoint.name)}})})}public unregister() {this.breakpoints.forEach((breakpoint: Breakpoint) => {if(breakpoint.mediaQueryListener){breakpoint.mediaQueryListener.off('change')}})}
}
declare interface BreakPointTypeOption<T> {xs?: Tsm?: Tmd?: Tlg?: Txl?: Txxl?: T
}export class BreakPointType<T> {options: BreakPointTypeOption<T>constructor(option: BreakPointTypeOption<T>) {this.options = option}getValue(currentBreakPoint: string) {if (currentBreakPoint === 'xs') {return this.options.xs} else if (currentBreakPoint === 'sm') {return this.options.sm} else if (currentBreakPoint === 'md') {return this.options.md} else if (currentBreakPoint === 'lg') {return this.options.lg} else if (currentBreakPoint === 'xl') {return this.options.xl} else if (currentBreakPoint === 'xxl') {return this.options.xxl} else {return undefined}}
}

5.3、栅格布局

在这里插入图片描述

5.3.1、缩进布局

在这里插入图片描述

5.3.2、挪移布局

在这里插入图片描述

5.3.3、重复布局

在这里插入图片描述

http://www.zhongyajixie.com/news/41253.html

相关文章:

  • 泰兴中信建设有限责任公司深圳网站搜索优化工具
  • 电商网站建设与运营方向就业前景深圳华强北新闻最新消息今天
  • 做展会怎么引流到自己的网站口碑营销经典案例
  • 东莞市网站建设分站品牌百度seo建议
  • 安徽省政府门户网站建设网站设计与制作公司
  • 自己制作网页的步骤南京seo排名公司
  • 宁波正规优化seo公司银川seo
  • 小公司做网站阿里指数查询
  • 模板之家怎么免费下载关键词seo服务
  • 广东软件开发公司班级优化大师简介
  • 全能网站建设淄博网站制作
  • 建站备案最常见企业网站有哪些
  • Wordpress网站仿站电商seo与sem是什么
  • 抖音代刷网站推广快速福州百度关键词排名
  • 丹东有做公司网站的吗太原网站建设谁家好
  • wordpress标签说明百度seo价格查询系统
  • 北京的电商平台网站宝鸡seo
  • 优质企业网站推广初学seo网站推广需要怎么做
  • 免费做公司手机网站域名注册需要哪些条件
  • html拖拽代码生成器seo广告优化多少钱
  • wordpress安装失败无法创建目录seo优化是利用规则提高排名
  • 传动设备 技术支持 东莞网站建设免费网站制作软件平台
  • 怎么用vs2008做网站朝阳网站seo
  • 上海网站公西安seo包年服务
  • 营销型网站建设的步骤站长工具站长之家官网
  • 落伍者论坛 做网站seo分析seo诊断
  • 山东众德建设项目管理公司网站站长工具传媒
  • 做微商那个网站好网站制作详细流程
  • wordpress换站线上营销方式主要有哪些
  • 在线制作图片拼图seo快速优化报价