vue.js 如何在作用域中指定名称和组件< script setup>

5us2dqdw  于 2023-05-18  发布在  Vue.js
关注(0)|答案(1)|浏览(221)

我想知道如何在<script setup>模式下定义namecomponents。在none <script setup模式下,我可以执行以下操作:

export default {
name: 'App',
props: ['foo','greetingMessage'],
components: {
    HelloWorld
},

我如何才能实现同样的事情在<script setup>模式请

App.vue

<template>
  <img alt="Vue logo" src="./assets/logo.png">
  <HelloWorld msg="Welcome to Your Vue.js App"/>
  <span>greetingMessage:{{ greetingMessage }}</span>
</template>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import {defineProps} from 'vue'

const props = defineProps(['foo','greetingMessage'])

name: 'App'
components: {
  HelloWorld
}

console.log("props:",props)
console.log("props.foo:",props.foo)
console.log("props.greetingMessage:",props.greetingMessage)

</script>
kulphzqa

kulphzqa1#

您可以只添加一个script标签,并在其中指定组件的名称。

就像这样:

<script>
export default {
  name: "MyComponentName",
};
</script>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import {defineProps} from 'vue'

const props = defineProps(['foo','greetingMessage'])

name: 'App'
components: {
  HelloWorld
}

console.log("props:",props)
console.log("props.foo:",props.foo)
console.log("props.greetingMessage:",props.greetingMessage)

</script>

相关问题