Skip to content

uni路由

该组件类似HTML中的<a>组件,但只能跳转本地页面。目标页面必须在pages.json中注册。

除了组件方式,API方式也可以实现页面跳转,另见:https://uniapp.dcloud.io/api/router?id=navigateto

属性说明

属性名类型默认值说明
urlString应用内的跳转链接,值为相对路径或绝对路径,如:"../first/first","/pages/first/first",注意不能加 .vue 后缀
open-typeStringnavigate跳转方式
deltaNumber当 open-type 为 'navigateBack' 时有效,表示回退的层数
animation-typeStringpop-in/out当 open-type 为 navigate、navigateBack 时有效,窗口的显示/关闭动画效果,详见:窗口动画
animation-durationNumber300当 open-type 为 navigate、navigateBack 时有效,窗口显示/关闭动画的持续时间。
render-linkbooleantrue是否给 navigator 组件加一层 a 标签控制 ssr 渲染
hover-classStringnavigator-hover指定点击时的样式类,当hover-class="none"时,没有点击态效果
hover-stop-propagationBooleanfalse指定是否阻止本节点的祖先节点出现点击态
hover-start-timeNumber50按住后多久出现点击态,单位毫秒
hover-stay-timeNumber600手指松开后点击态保留时间,单位毫秒
targetStringself在哪个小程序目标上发生跳转,默认当前小程序,值域self/miniProgram

open-type 有效值

说明
navigate对应 uni.navigateTo 的功能
redirect对应 uni.redirectTo 的功能
switchTab对应 uni.switchTab 的功能
reLaunch对应 uni.reLaunch 的功能
navigateBack对应 uni.navigateBack 的功能
exit退出小程序,target="miniProgram"时生效

路由跳转

ts
页面的跳转方式有两种

编程式导航	类似于vue中 router.push(...)
声明式导航 	类似于vue中 <router-link :to="...">
ts
新建一个文件夹detail,用于存放详情的页面
设置页面的标题

声明式导航

ts
navigitor 组件
[属性]
url				当前小程序内的跳转链接
open-type		跳转方式
xml
<navigitor url="" open-type="navigate" ></navigitor>
ts
open-type = "reLaunch" 关闭所有页面,打开到应用内的某个页面(能跳到tabbar)
open-type = "navigate" 保留当前页面,跳转到应用内的某个页面(不能跳到tabbar),默认
open-type = "redirect" 关闭当前页面,跳转到应用内的某个页面(不能跳到tabbar)
ts
<navigator url="/pages/shopcart/shopcart" open-type="reLaunch">
    <view class="icon_wrap flex">
        <van-icon name="shopping-cart" color="#666" size="22px"></van-icon>
        <text>购物车</text>
    </view>
</navigator>

uni请求

uni.request(OBJECT)

发起网络请求。

OBJECT 参数说明

参数名类型必填默认值说明
urlString开发者服务器接口地址
dataObject/String/ArrayBuffer请求的参数
headerObject设置请求的 header,header 中不能设置 Referer
methodStringGET有效值详见下方说明
timeoutNumber60000超时时间,单位 ms
dataTypeStringjson如果设为 json,会对返回的数据进行一次 JSON.parse,非 json 不会进行 JSON.parse
responseTypeStringtext设置响应的数据类型。合法值:text、arraybuffer
sslVerifyBooleantrue验证 ssl 证书
withCredentialsBooleanfalse跨域请求时是否携带凭证(cookies)
firstIpv4BooleanfalseDNS解析时优先使用ipv4
enableHttp2Booleanfalse开启 http2
enableQuicBooleanfalse开启 quic
enableCacheBooleanfalse开启 cache
enableHttpDNSBooleanfalse是否开启 HttpDNS 服务。如开启,需要同时填入 httpDNSServiceId 。 HttpDNS 用法详见 移动解析HttpDNS
httpDNSServiceIdStringHttpDNS 服务商 Id。 HttpDNS 用法详见 移动解析HttpDNS
enableChunkedBooleanfalse开启 transfer-encoding chunked
forceCellularNetworkBooleanfalsewifi下使用移动网络发送请求
enableCookieBooleanfalse开启后可在headers中编辑cookie
cloudCacheObject/Booleanfalse是否开启云加速(详见云加速服务
deferBooleanfalse控制当前请求是否延时至首屏内容渲染后发送
successFunction收到开发者服务器成功返回的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数类型说明
dataObject/String/ArrayBuffer开发者服务器返回的数据
statusCodeNumber开发者服务器返回的 HTTP 状态码
headerObject开发者服务器返回的 HTTP Response Header
cookies开发者服务器返回的 cookies,格式为字符串数组

示例

ts
写法方式1
function request(){
	uni.request({
		url:"https://jsonplaceholder.typicode.com/posts",
		success:res=>{
			console.log(res);
			arrs.value = res.data 
		}
	})
}
写法方式2
function request(){
	uni.request({
		url:"https://jsonplaceholder.typicode.com/posts"
	}).then(res=>{
		arrs.value = res.data 
	})
}
写法方式3
async function request(){
	let res = await uni.request({
		url:"https://jsonplaceholder.typicode.com/posts"
	})	
	arrs.value = res.data 
}

请求参数

data 数据说明

最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:

  • 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18
  • 对于 POST 方法且 header['content-type']application/json 的数据,会进行 JSON 序列化。
  • 对于 POST 方法且 header['content-type']application/x-www-form-urlencoded 的数据,会将数据转换为 query string。

示例

javascript
uni.request({
    url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。
    data: {
        text: 'uni.request'
    },
    header: {
        'custom-header': 'hello', //自定义请求头信息
        'token':'xxxx'
    },
    method:"get",
    success: (res) => {
        console.log(res.data);
        this.text = 'request success';
    }
});

返回值

如果希望返回一个 requestTask 对象,需要至少传入 success / fail / complete 参数中的一个。例如:

javascript
var requestTask = uni.request({
	url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。
	complete: ()=> {}
});
requestTask.abort();

如果没有传入 success / fail / complete 参数,则会返回封装后的 Promise 对象:Promise 封装

通过 requestTask,可中断请求任务。

requestTask.abort()

如果没有传入 success / fail / complete 参数,则会返回封装后的 Promise 对象:Promise 封装

通过 requestTask,可中断请求任务。