佛山高端网站建设工作室wordpress化妆品主题
web/
2025/9/27 6:01:52/
文章来源:
佛山高端网站建设工作室,wordpress化妆品主题,南昌网站建设如何,舟山做网站的公司在Vue 3 中有多种定义组件的方法。从选项到组合再到类 API#xff0c;情况大不相同
1、方式一#xff1a;Options API
这是在 Vue 中声明组件的最常见方式。从版本 1 开始可用#xff0c;您很可能已经熟悉它。一切都在对象内声明#xff0c;数据在幕后由 Vue 响应。它不是…在Vue 3 中有多种定义组件的方法。从选项到组合再到类 API情况大不相同
1、方式一Options API
这是在 Vue 中声明组件的最常见方式。从版本 1 开始可用您很可能已经熟悉它。一切都在对象内声明数据在幕后由 Vue 响应。它不是那么灵活因为它使用 mixin 来共享行为。 script
import TheComponent from ./components/TheComponent.vue
import componentMixin from ./mixins/componentMixin.jsexport default {name: OptionsAPI,components: {TheComponent,AsyncComponent: () import(./components/AsyncComponent.vue),},mixins: [componentMixin],props: {elements: {type: Array,},counter: {type: Number,default: 0,},},data() {return {object: {variable: true,},}},computed: {isEmpty() {return this.counter 0},},watch: {counter() {console.log(Counter value changed)},},created() {console.log(Created hook called)},mounted() {console.log(Mounted hook called)},methods: {getParam(param) {return param},emitEvent() {this.$emit(event-name)},},
}
/script
templatediv classwrapperTheComponent /AsyncComponent v-ifobject.variable /div classstatic-class-name :class{ dynamic-class-name: object.variable }Dynamic attributes example/divbutton clickemitEventEmit event/button/div
/templatestyle langscss scoped
.wrapper {font-size: 20px;
}
/style方式二Composition API
在 Vue 3 中引入了 Composition API。 目的是提供更灵活的 API 和更好的 TypeScript 支持。这种方法在很大程度上依赖于设置生命周期挂钩。 script
import {ref,reactive,defineComponent,computed,watch,
} from vueimport useMixin from ./mixins/componentMixin.js
import TheComponent from ./components/TheComponent.vueexport default defineComponent({name: CompositionAPI,components: {TheComponent,AsyncComponent: () import(./components/AsyncComponent.vue),},props: {elements: Array,counter: {type: Number,default: 0,},},setup(props, { emit }) {console.log(Equivalent to created hook)const enabled ref(true)const object reactive({ variable: false })const { mixinData, mixinMethod } useMixin()const isEmpty computed(() {return props.counter 0})watch(() props.counter,() {console.log(Counter value changed)})function emitEvent() {emit(event-name)}function getParam(param) {return param}return {object,getParam,emitEvent,isEmpty}},mounted() {console.log(Mounted hook called)},
})
/scripttemplatediv classwrapperTheComponent /AsyncComponent v-ifobject.variable /div classstatic-class-name :class{ dynamic-class-name: object.variable }Dynamic attributes example/divbutton clickemitEventEmit event/button/div
/templatestyle scoped
.wrapper {font-size: 20px;
}
/style使用这种混合方法需要大量样板代码而且设置函数很快就会失控。在迁移到 Vue 3 时这可能是一个很好的中间步骤但是语法糖可以让一切变得更干净。
方式三Script setup
在 Vue 3.2 中引入了一种更简洁的语法。通过在脚本元素中添加设置属性脚本部分中的所有内容都会自动暴露给模板。通过这种方式可以删除很多样板文件。 script setup
import {ref,reactive,defineAsyncComponent,computed,watch,onMounted,
} from vue;import useMixin from ./mixins/componentMixin.js;
import TheComponent from ./components/TheComponent.vue;
const AsyncComponent defineAsyncComponent(() import(./components/AsyncComponent.vue)
);console.log(Equivalent to created hook);
onMounted(() {console.log(Mounted hook called);
});const enabled ref(true);
const object reactive({ variable: false });const props defineProps({elements: Array,counter: {type: Number,default: 0,},
});const { mixinData, mixinMethod } useMixin();const isEmpty computed(() {return props.counter 0;
});watch(() props.counter, () {console.log(Counter value changed);
});const emit defineEmits([event-name]);
function emitEvent() {emit(event-name);
}
function getParam(param) {return param;
}
/scriptscript
export default {name: ComponentVue3,
};
/scripttemplatediv classwrapperTheComponent /AsyncComponent v-ifobject.variable /divclassstatic-class-name:class{ dynamic-class-name: object.variable }Dynamic attributes example/divbutton clickemitEventEmit event/button/div
/templatestyle scoped
.wrapper {font-size: 20px;
}
/style
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82579.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!