提交 83250d87 编写于 作者: W wanganxp

更新context和泛型值传递问题

上级 742836c7
......@@ -2,10 +2,35 @@
<!-- UTSAPIJSON.createVideoContext.description -->
参考:[video组件](../component/video.md)
<!-- UTSAPIJSON.createVideoContext.compatibility -->
<!-- UTSAPIJSON.createVideoContext.param -->
在video组件上设置id属性,即可通过本API获取该组件的上下文对象(videoContext )。
在不传入第2个component参数时,默认从页面栈顶的页面来查找这个video。
```js
let v1 = uni.createVideoContext("video1")
```
但在页面、组件,可能出现id重复的情况,还可能因为调用时机的不同,想定位不同页面的video id。此时就需要传入第2个参数component。
所有vue组件都有ComponentPublicInstance实例,页面上也有组件实例。
如果传入组件实例,就从这个组件中查询指定的id。这样查找范围更小、查找速度也更快。
在选项式和组合式中,获取组件实例略有差别。
- 选项式中,this代表当前vue实例。如果是在页面的export default内、函数外使用this,即代表该页面的组件实例。
```js
let v1 = uni.createVideoContext("video1", this)
```
- 组合式中,通过getCurrentInstance()!.proxy! 获取当前页面的组件实例。
```js
let v1 = uni.createVideoContext("video1", getCurrentInstance()!.proxy!)
```
<!-- UTSAPIJSON.createVideoContext.returnValue -->
<!-- UTSAPIJSON.createVideoContext.example -->
......@@ -16,4 +41,4 @@
<!-- UTSAPIJSON.general_type.param -->
示例代码另见[video组件](../component/video.md)
示例代码另见[video组件](../component/video.md)
\ No newline at end of file
......@@ -2,6 +2,26 @@
<!-- UTSAPIJSON.createWebviewContext.description -->
由于本API仅在App端支持。推荐直接使用DOM API操作。UniWebViewElement比webviewContext有更多属性和方法。例如:
```js
let wv = uni.getElementById("wv1") as UniWebViewElement
```
如果您继续使用createWebviewContext,请注意第2个参数问题。
在不传入第2个component参数时,默认从页面栈顶的页面来查找这个webviewContext。
在选项式和组合式中,获取组件实例略有差别。
- 选项式中,this代表当前vue实例。如果是在页面的export default内、函数外使用this,即代表该页面的组件实例。
```js
let v1 = uni.createWebviewContext("wv1", this)
```
- 组合式中,通过getCurrentInstance()!.proxy! 获取当前页面的组件实例。
```js
let v1 = uni.createWebviewContext("wv1", getCurrentInstance()!.proxy!)
```
<!-- UTSAPIJSON.createWebviewContext.compatibility -->
<!-- UTSAPIJSON.createWebviewContext.param -->
......
......@@ -17,17 +17,18 @@
## 注意事项
* app-android平台 request 接口内部通过[特殊方式读取了泛型类型](../uts/generics.md#使用限制),不支持传入动态的泛型:比如将外层方法的普通泛型参数传入 request。
* 推荐使用成熟的网络拦截器插件,见[插件市场](https://ext.dcloud.net.cn/search?q=%E7%BD%91%E7%BB%9C%E6%8B%A6%E6%88%AA%E5%99%A8&uni-appx=1)
* app-android平台 request 接口如需包装和传递泛型,需参考[泛型传递丢失注意](../plugin/uts-for-android.md#6.6 泛型传递丢失的问题)。成熟的拦截器插件均已自动处理这些问题。
* 如果使用泛型先创建RequestOptions实例,再传入uni.request(),此时请务必确保request要显式指定泛型,例:
```typescript
const options: RequestOptions<Person> = ...
uni.request<Person>(options)
```
```typescript
const options: RequestOptions<Person> = ...
uni.request<Person>(options)
```
* app-android平台 uni.request()暂未支持Promise,返回值是RequestTask。
* web平台 request接口在 4.01版本之前返回数据是一个普通对象,4.01起调整为UTSJSONObject类型
* web平台 request接口目前不支持创建传入的泛型的实例
由于uni-app x的强类型,导致联网相关开发有一些不同,请参考完整教程:[uni-app x的联网教程](../tutorial/request.md)
由于uni-app x的强类型,导致联网相关开发有一些不同,请不熟悉强类型的开发者务必阅读教程:[uni-app x的联网教程](../tutorial/request.md)
<!-- UTSAPIJSON.general_type.name -->
......
......@@ -80,10 +80,9 @@ function testArray<T extends Array<unknown>>(arg: T): T {
}
```
## 使用限制
## 注意
普通泛型类型不可以作为值使用。
ts本质是擦除类型的。在强类型语言中,传递泛型时如将泛型类型作为值使用,需通过特殊方式将泛型类型作为值使用,可以根据传入来动态决定返回类型。[详见](../plugin/uts-for-android.md#6.6 泛型传递丢失的问题)
> HBuilderX 3.9.0+ 支持通过特殊方式将泛型类型作为值使用(暂未开放),方法可以根据传入来动态决定返回类型。
>
> 这个未开放的泛型用法最常用的是在request api里,[详见](../tutorial/request.md)
> 泛型用法最常用的是在request api里,[详见](../tutorial/request.md)
> 网络请求包装推荐使用成熟的网络拦截器插件,见[插件市场](https://ext.dcloud.net.cn/search?q=%E7%BD%91%E7%BB%9C%E6%8B%A6%E6%88%AA%E5%99%A8&uni-appx=1)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册