How to use props in <script setup> in vue3

Viewed 50109

like Title, about 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>
7 Answers

To use props with <script setup> you need to call defineProps() with the component prop options as the argument, this defines the props on the component instance and returns a reactive object with the props which you can use as follows:

<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>

If you are using typescript the alternative way to do this is pass a type only declaration and infer the prop types from that. Pro's are that you'll get stricter type safety but you cannot have default values.

<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>

EDIT

Defaults with type only props are now possible:

interface Props {
  msg?: string
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello'
})

Very simple answer using features from Vue-3.1 and later:

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>

See also the documentation: Declaring props or additional options

Edit: This answer is outdated. The RFC was changed to use defineProps(..), which is auto-imported if using SFCs.

As per the ongoing RFC, the setup tag can have a string following where you can define which context you wish to have, like so:

<script setup="props, { emit }">
import { watchEffect } from 'vue';

watchEffect(() => console.log(props.msg));
emit('foo');
</script>

These are the same arguments that the setup() method receives.

No need to call explicitly withDefaults

<script setup>
import { defineProps } from 'vue'

defineProps({
  isOpen: {
    type: Boolean,
    default: true
  }
})
</script>

I read "Newscript setup" and found the answer

first,use variable save defineProps

const props = defineProps({
  no: String
})

then use it

const init = async () => {
  console.log(props.no);
}

this is all code:

<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>

Using Typescript you could define your props in two syntaxes :

  1. First syntax

<script setup lang="ts">
import {PropType} from 'vue'

const props=defineProps({
   color:{
      type:String as PropType<'primary'|'info'|'success'|'error'|'warning'>,
      default :'primary'
    }
})

</script>

  1. Second syntax

<script setup lang="ts">

type Color='primary'|'info'|'success'|'error'|'warning'

const props=defineProps<{color:Color}>()

</script>

This syntax supports only :

  • A type literal
  • A reference to an interface or a type literal in the same file

If you're using Typescript and also want to add a default value, you can use this syntax.

<script setup lang="ts">

interface Props {
 disabled?: boolean;
 loading?: boolean;
 bordered?: boolean;
 label: string;
}

const props = withDefaults(defineProps<Props>(), {
 label: "Button Label",
}); 
</script>
Related