提交 52073af8 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: 补充 props 使用引入类型示例

上级 9dc66aae
<template> <template>
<view class="page"> <view class="page">
<array-literal :str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" /> <array-literal :str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" />
<object-type str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" /> <object-type str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" />
<same-name-prop-default-value /> <same-name-prop-default-value />
<props-with-defaults /> <props-with-defaults />
</view> <!-- #ifdef APP-ANDROID -->
</template> <reference-types :list="[1,2,3]" />
<!-- #endif -->
<script setup lang="uts"> <!-- #ifndef APP-ANDROID -->
import ArrayLiteral from './array-literal-composition.uvue' <reference-types :list="['a','b','c']" />
import ObjectType from "./object-type-composition.uvue"; <!-- #endif -->
import SameNamePropDefaultValue from "./same-name-prop-default-value-composition.uvue"; </view>
import PropsWithDefaults from "./props-with-defaults.uvue"; </template>
const str = 'str' <script setup lang="uts">
const num = 10 import ArrayLiteral from './array-literal-composition.uvue'
const bool = true import ObjectType from "./object-type-composition.uvue";
const obj = {age: 18} import SameNamePropDefaultValue from "./same-name-prop-default-value-composition.uvue";
const arr = ['a','b','c'] import PropsWithDefaults from "./props-with-defaults.uvue";
</script> import ReferenceTypes from './reference-types-composition.uvue'
const str = 'str'
const num = 10
const bool = true
const obj = { age: 18 }
const arr = ['a', 'b', 'c']
</script>
\ No newline at end of file
<template> <template>
<view class="page"> <view class="page">
<array-literal :str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" /> <array-literal :str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" />
<object-type str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" /> <object-type str="str" :num="num" :bool="bool" :obj="obj" :arr="arr" />
<same-name-prop-default-value /> <same-name-prop-default-value />
<props-with-defaults /> <props-with-defaults />
</view> <!-- #ifdef APP-ANDROID -->
</template> <reference-types :list="[1,2,3]" />
<!-- #endif -->
<script lang="uts"> <!-- #ifndef APP-ANDROID -->
import ArrayLiteral from './array-literal-options.uvue' <reference-types :list="['a','b','c']" />
import ObjectType from "./object-type-options.uvue"; <!-- #endif -->
import SameNamePropDefaultValue from "./same-name-prop-default-value-options.uvue"; </view>
import PropsWithDefaults from "./props-with-defaults.uvue"; </template>
export default { <script lang="uts">
components: { import ArrayLiteral from './array-literal-options.uvue'
ArrayLiteral, import ObjectType from "./object-type-options.uvue";
ObjectType, import SameNamePropDefaultValue from "./same-name-prop-default-value-options.uvue";
SameNamePropDefaultValue, import PropsWithDefaults from "./props-with-defaults.uvue";
PropsWithDefaults import ReferenceTypes from './reference-types-options.uvue'
},
data() { export default {
return { components: {
str: 'str', ArrayLiteral,
num: 10, ObjectType,
bool: true, SameNamePropDefaultValue,
obj: {age: 18}, PropsWithDefaults,
arr: ['a','b','c'] ReferenceTypes
} },
}, data() {
} return {
</script> str: 'str',
num: 10,
bool: true,
obj: { age: 18 },
arr: ['a', 'b', 'c']
}
},
}
</script>
\ No newline at end of file
const OPTIONS_PAGE_PATH = '/pages/component-instance/props/props-options' const OPTIONS_PAGE_PATH = '/pages/component-instance/props/props-options'
const COMPOSITION_PAGE_PATH = '/pages/component-instance/props/props-composition' const COMPOSITION_PAGE_PATH = '/pages/component-instance/props/props-composition'
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isAndroid = platformInfo.includes('android')
describe('props', () => { describe('props', () => {
let page let page
const test = async (page) => { const test = async (page) => {
const arrayLiteralStr = await page.$('#array-literal-str') const arrayLiteralStr = await page.$('#array-literal-str')
expect(await arrayLiteralStr.text()).toBe('str') expect(await arrayLiteralStr.text()).toBe('str')
const arrayLiteralNum = await page.$('#array-literal-num') const arrayLiteralNum = await page.$('#array-literal-num')
expect(await arrayLiteralNum.text()).toBe('10') expect(await arrayLiteralNum.text()).toBe('10')
const arrayLiteralBool = await page.$('#array-literal-bool') const arrayLiteralBool = await page.$('#array-literal-bool')
expect(await arrayLiteralBool.text()).toBe('true') expect(await arrayLiteralBool.text()).toBe('true')
const arrayLiteralObj = await page.$('#array-literal-obj') const arrayLiteralObj = await page.$('#array-literal-obj')
expect(await arrayLiteralObj.text()).toBe('{"age":18}') expect(await arrayLiteralObj.text()).toBe('{"age":18}')
const arrayLiteralArr = await page.$('#array-literal-arr') const arrayLiteralArr = await page.$('#array-literal-arr')
expect(await arrayLiteralArr.text()).toBe('["a","b","c"]') expect(await arrayLiteralArr.text()).toBe('["a","b","c"]')
const objectTypeStr = await page.$('#object-type-str') const objectTypeStr = await page.$('#object-type-str')
expect(await objectTypeStr.text()).toBe('str') expect(await objectTypeStr.text()).toBe('str')
const objectTypeNum = await page.$('#object-type-num') const objectTypeNum = await page.$('#object-type-num')
expect(await objectTypeNum.text()).toBe('10') expect(await objectTypeNum.text()).toBe('10')
const objectTypeBool = await page.$('#object-type-bool') const objectTypeBool = await page.$('#object-type-bool')
expect(await objectTypeBool.text()).toBe('true') expect(await objectTypeBool.text()).toBe('true')
const objectTypeObj = await page.$('#object-type-obj') const objectTypeObj = await page.$('#object-type-obj')
expect(await objectTypeObj.text()).toBe('{"age":18}') expect(await objectTypeObj.text()).toBe('{"age":18}')
const objectTypeArr = await page.$('#object-type-arr') const objectTypeArr = await page.$('#object-type-arr')
expect(await objectTypeArr.text()).toBe('["a","b","c"]') expect(await objectTypeArr.text()).toBe('["a","b","c"]')
const sameNamePropDefaultValueArr = await page.$('#same-name-prop-default-value-arr') const sameNamePropDefaultValueArr = await page.$('#same-name-prop-default-value-arr')
expect(await sameNamePropDefaultValueArr.text()).toBe('[1,2,3]') expect(await sameNamePropDefaultValueArr.text()).toBe('[1,2,3]')
...@@ -41,19 +44,22 @@ describe('props', () => { ...@@ -41,19 +44,22 @@ describe('props', () => {
expect(await propMsg.text()).toBe('hello') expect(await propMsg.text()).toBe('hello')
const propLabels = await page.$('#prop-labels') const propLabels = await page.$('#prop-labels')
expect(await propLabels.text()).toBe('["a","b"]') expect(await propLabels.text()).toBe('["a","b"]')
const referenceTypeList = await page.$('#reference-type-list')
expect(await referenceTypeList.text()).toBe(isAndroid ? '[1,2,3]' : '["a","b","c"]')
} }
it('props 选项式 API', async () => { it('props 选项式 API', async () => {
page = await program.reLaunch(OPTIONS_PAGE_PATH) page = await program.reLaunch(OPTIONS_PAGE_PATH)
await page.waitFor('view') await page.waitFor('view')
await test(page) await test(page)
}); });
it('props 组合式 API', async () => { it('props 组合式 API', async () => {
page = await program.reLaunch(COMPOSITION_PAGE_PATH) page = await program.reLaunch(COMPOSITION_PAGE_PATH)
await page.waitFor('view') await page.waitFor('view')
await test(page) await test(page)
}) })
}) })
\ No newline at end of file
<template>
<view>
<text class="mb-10 bold">reference types</text>
<text id="reference-type-list">{{ JSON.stringify(list) }}</text>
</view>
</template>
<script setup lang="uts">
import type { ReferenceTypesProps } from './types.uts'
const props = withDefaults(defineProps<ReferenceTypesProps>(), {
// #ifdef APP-ANDROID
list: [] as number[]
// #endif
// #ifndef APP-ANDROID
list: [] as string[]
// #endif
})
console.log('reference types props', props)
</script>
\ No newline at end of file
<template>
<view>
<text class="mb-10 bold">reference types</text>
<text id="reference-type-list">{{ JSON.stringify(list) }}</text>
</view>
</template>
<script lang="uts">
export default {
props: {
list: {
// #ifdef APP-ANDROID
type: Array as PropType<number[]>,
default: [] as number[]
// #endif
// #ifndef APP-ANDROID
type: Array as PropType<string[]>,
default: [] as string[]
// #endif
}
},
onReady() {
console.log('reference types props', this.$props)
}
}
</script>
\ No newline at end of file
export type ReferenceTypesProps = {
// #ifdef APP-ANDROID
list: number[]
// #endif
// #ifndef APP-ANDROID
list: string[]
// #endif
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册