如何在vue3中使用<script setup>中的道具
How to use props in <script setup> in vue3
喜欢标题,
关于 link:
New script setup
(without ref sugar)
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from '@/layout/TopNavbar.vue'
import { defineProps, reactive } from 'vue'
defineProps({
no: String
})
const state = reactive({
room: {}
})
const init = async () => {
// I want use props in this
// const { data } = await getRoomByNo(props.no)
// console.log(data);
}
init()
</script>
<style>
</style>
我阅读了“新script setup
”并找到了答案
首先,使用变量保存defineProps
const props = defineProps({
no: String
})
然后使用它
const init = async () => {
console.log(props.no);
}
这是全部代码:
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from '@/layout/TopNavbar.vue'
import { defineProps, reactive, useContext } from 'vue'
const props = defineProps({
no: String
})
const state = reactive({
room: {}
})
const init = async () => {
console.log(props.no);
}
init()
</script>
<style>
</style>
要将 props 与 <script setup>
一起使用,您需要调用 defineProps()
并将组件 prop 选项作为参数,这定义了组件实例上的 props 和 returns a reactive
对象与您可以使用的道具如下:
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from "@/layout/TopNavbar.vue";
import { defineProps, reactive } from "vue";
const props = defineProps({
no: String,
});
const { no } = toRefs(props);
const state = reactive({
room: {},
});
const init = async () => {
const { data } = await getRoomByNo(no.value);
console.log(data);
};
init();
</script>
如果您使用的是打字稿,另一种方法是传递仅类型声明并从中推断道具类型。优点是您将获得更严格的类型安全但您不能有默认值。
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from "@/layout/TopNavbar.vue";
import { defineProps, reactive } from "vue";
const props = defineProps<{
no: string,
}>();
const { no } = toRefs(props);
const state = reactive({
room: {},
});
const init = async () => {
const { data } = await getRoomByNo(no.value);
console.log(data);
};
init();
</script>
编辑
现在可以使用仅类型道具的默认值:
interface Props {
msg?: string
}
const props = withDefaults(defineProps<Props>(), {
msg: 'hello'
})
编辑:此答案已过时。 RFC 已更改为使用 defineProps(..)
,如果使用 SFC,它会自动导入。
根据 ongoing RFC,setup
标签可以有一个字符串,您可以在其中定义您希望拥有的上下文,如下所示:
<script setup="props, { emit }">
import { watchEffect } from 'vue';
watchEffect(() => console.log(props.msg));
emit('foo');
</script>
这些是 setup()
方法接收的相同参数。
使用 Vue-3.1 及更高版本的功能的非常简单的答案:
CircleImage.view
<template>
<div class="px-4 w-8/12 sm:w-3/12">
<img :src="src" :alt="alt" class="border-none rounded-full h-auto max-w-full align-middle" />
</div>
</template>
<script setup>
const props = defineProps({
src: String,
alt: String,
})
</script>
MyView.view
<template>
<div class="flex flex-wrap justify-center">
<CircleImage src="/file1.jpg" alt="one" />
<CircleImage src="/file2.svg" alt="two" />
</div>
</template>
<script setup>
import CircleImage from '@/components/CircleImage.vue'
</script>
无需显式调用withDefaults
<script setup>
import { defineProps } from 'vue'
defineProps({
isOpen: {
type: Boolean,
default: true
}
})
</script>
使用 Typescript 你可以用两种语法定义你的道具:
- 第一个语法
<script setup lang="ts">
import {PropType} from 'vue'
const props=defineProps({
color:{
type:String as PropType<'primary'|'info'|'success'|'error'|'warning'>,
default :'primary'
}
})
</script>
- 第二种语法
<script setup lang="ts">
type Color='primary'|'info'|'success'|'error'|'warning'
const props=defineProps<{color:Color}>()
</script>
此语法仅支持:
- 类型文字
- 对同一文件中接口或类型文字的引用
喜欢标题,
关于 link:
New script setup
(without ref sugar)
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from '@/layout/TopNavbar.vue'
import { defineProps, reactive } from 'vue'
defineProps({
no: String
})
const state = reactive({
room: {}
})
const init = async () => {
// I want use props in this
// const { data } = await getRoomByNo(props.no)
// console.log(data);
}
init()
</script>
<style>
</style>
我阅读了“新script setup
”并找到了答案
首先,使用变量保存defineProps
const props = defineProps({
no: String
})
然后使用它
const init = async () => {
console.log(props.no);
}
这是全部代码:
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from '@/layout/TopNavbar.vue'
import { defineProps, reactive, useContext } from 'vue'
const props = defineProps({
no: String
})
const state = reactive({
room: {}
})
const init = async () => {
console.log(props.no);
}
init()
</script>
<style>
</style>
要将 props 与 <script setup>
一起使用,您需要调用 defineProps()
并将组件 prop 选项作为参数,这定义了组件实例上的 props 和 returns a reactive
对象与您可以使用的道具如下:
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from "@/layout/TopNavbar.vue";
import { defineProps, reactive } from "vue";
const props = defineProps({
no: String,
});
const { no } = toRefs(props);
const state = reactive({
room: {},
});
const init = async () => {
const { data } = await getRoomByNo(no.value);
console.log(data);
};
init();
</script>
如果您使用的是打字稿,另一种方法是传递仅类型声明并从中推断道具类型。优点是您将获得更严格的类型安全但您不能有默认值。
<template>
<TopNavbar title="room" />
<div>
{{ no }}
</div>
</template>
<script setup>
import TopNavbar from "@/layout/TopNavbar.vue";
import { defineProps, reactive } from "vue";
const props = defineProps<{
no: string,
}>();
const { no } = toRefs(props);
const state = reactive({
room: {},
});
const init = async () => {
const { data } = await getRoomByNo(no.value);
console.log(data);
};
init();
</script>
编辑
现在可以使用仅类型道具的默认值:
interface Props {
msg?: string
}
const props = withDefaults(defineProps<Props>(), {
msg: 'hello'
})
编辑:此答案已过时。 RFC 已更改为使用 defineProps(..)
,如果使用 SFC,它会自动导入。
根据 ongoing RFC,setup
标签可以有一个字符串,您可以在其中定义您希望拥有的上下文,如下所示:
<script setup="props, { emit }">
import { watchEffect } from 'vue';
watchEffect(() => console.log(props.msg));
emit('foo');
</script>
这些是 setup()
方法接收的相同参数。
使用 Vue-3.1 及更高版本的功能的非常简单的答案:
CircleImage.view
<template>
<div class="px-4 w-8/12 sm:w-3/12">
<img :src="src" :alt="alt" class="border-none rounded-full h-auto max-w-full align-middle" />
</div>
</template>
<script setup>
const props = defineProps({
src: String,
alt: String,
})
</script>
MyView.view
<template>
<div class="flex flex-wrap justify-center">
<CircleImage src="/file1.jpg" alt="one" />
<CircleImage src="/file2.svg" alt="two" />
</div>
</template>
<script setup>
import CircleImage from '@/components/CircleImage.vue'
</script>
无需显式调用withDefaults
<script setup>
import { defineProps } from 'vue'
defineProps({
isOpen: {
type: Boolean,
default: true
}
})
</script>
使用 Typescript 你可以用两种语法定义你的道具:
- 第一个语法
<script setup lang="ts">
import {PropType} from 'vue'
const props=defineProps({
color:{
type:String as PropType<'primary'|'info'|'success'|'error'|'warning'>,
default :'primary'
}
})
</script>
- 第二种语法
<script setup lang="ts">
type Color='primary'|'info'|'success'|'error'|'warning'
const props=defineProps<{color:Color}>()
</script>
此语法仅支持:
- 类型文字
- 对同一文件中接口或类型文字的引用