当前位置: 首页 > news >正文

免费视频素材下载的网站站长之家点击进入

免费视频素材下载的网站,站长之家点击进入,网站开发a ajax,查企业的app软件有哪些在vue3中定义组件的5种方式 Vue 正在不断发展,目前在 Vue3 中定义组件的方法有多种。从选项式到组合式再到类API,情况截然不同。本文将会定义一个简单的组件并使用所有可用的方法重构它。 选项式 这是在 Vue 中声明组件的最常见方法。从 Vue1 就开始存…

在vue3中定义组件的5种方式

Vue 正在不断发展,目前在 Vue3 中定义组件的方法有多种。从选项式到组合式再到类API,情况截然不同。本文将会定义一个简单的组件并使用所有可用的方法重构它。

选项式

这是在 Vue 中声明组件的最常见方法。从 Vue1 就开始存在了,我们很可能已经熟悉它了。一切都在对象内部声明,并且数据在Vue中会定义成响应式。这种方式不是那么灵活,因为它使用 mixins 来共享行为。

<script>
import TheComponent from './components/TheComponent.vue'
import componentMixin from './mixins/componentMixin.js'export 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>
<template><div class="wrapper"><TheComponent /><AsyncComponent v-if="object.variable" /><div class="static-class-name" :class="{ 'dynamic-class-name': object.variable }">动态数据</div><button @click="emitEvent">触发事件</button></div>
</template><style lang="scss" scoped>
.wrapper {font-size: 20px;
}
</style>

使用这种混合方法,需要大量样板代码,并且设置的功能会随着项目越来越大越难以维护。

组合式

Vue3 中引入了 Composition API 。目的自然是提供更灵活的 API 和更好的 TypeScript 支持。这种方法在很大程度上依赖于安装生命周期挂钩(hooks)。

<script>
import {ref,reactive,defineComponent,computed,watch,
} from 'vue'import useMixin from './mixins/componentMixin.js'
import TheComponent from './components/TheComponent.vue'export 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')},
})
</script><template><div class="wrapper"><TheComponent /><AsyncComponent v-if="object.variable" /><div class="static-class-name" :class="{ 'dynamic-class-name': object.variable }">动态数据</div><button @click="emitEvent">触发事件</button></div>
</template><style scoped>
.wrapper {font-size: 20px;
}
</style>

使用组合式的方式可以项目逻辑更加清晰。

script setup

Vue 3.2 中引入了更简洁的语法。通过在 script 标签中添加 setup 属性,脚本部分中的所有内容都会自动暴露给模板。通过这种方式同样可以删除很多样板文件。

<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;
}
</script><script>
export default {name: "ComponentVue3",
};
</script><template><div class="wrapper"><TheComponent /><AsyncComponent v-if="object.variable" /><div class="static-class-name" :class="{ 'dynamic-class-name': object.variable }">动态数据</div><button @click="emitEvent">触发事件</button></div>
</template><style scoped>
.wrapper {font-size: 20px;
}
</style>

响应性语法糖

2023 年 1 月 26 日更新:这是非常有争议的,因此被删除!不过我们也可以稍微了解一下

以下使用script setup代码片段中演示的内容存在问题:

<script setup>
import { ref, computed } from 'vue'const counter = ref(0)
counter.value++function increase() {counter.value++
}const double = computed(() => {return counter.value * 2
})
</script><template><div class="wrapper"><button @click="increase">Increase</button>{{ counter }}{{ double }}</div>
</template>

使用.value访问反应式计数器感觉不自然,并且是造成混乱和错误输入的常见原因。有一个实验性解决方案利用编译时转换来解决此问题。反应性转换是一个可选的内置步骤,它会自动添加此后缀并使代码看起来更干净。

<script setup>
import { computed } from 'vue'let counter = $ref(0)
counter++function increase() {counter++
}const double = computed(() => {return counter * 2
})
</script><template><div class="wrapper"><button @click="increase">Increase</button>{{ counter }}{{ double }}</div>
</template>

$ref.value需要构建步骤,但消除了访问变量时的必要性。启用后,它在全局范围内可用。

class api

Class API 已经存在很长时间了。通常与 Typescript 搭配使用。并且被认真考虑过作为默认的 Vue 3 语法。但经过多次长时间的讨论后,它被放弃了,取而代之的是 Composition API。它在 Vue 3 中可用,但工具明显缺乏,官方建议放弃它。

<script lang="ts">
import { Options, Vue } from 'vue-class-component';import AnotherComponent from './components/AnotherComponent.vue'  @Options({components: {AnotherComponent}
})
export default class Counter extends Vue {counter = 0;get double(): number {return this.counter * 2;}increase(): void {this.quantity++;}
}
</script><template><div class="wrapper"><button @click="increase">Increase</button>{{ counter }}{{ double }}</div>
</template>
http://www.ds6.com.cn/news/59784.html

相关文章:

  • 信息分类网站好建吗长沙弧度seo
  • 查看一个网站的源代码做评价seo 什么意思
  • 太原专业设计网页公司上海seo服务外包公司
  • 西安网站建设哪家公司好宁波seo搜索引擎优化公司
  • 昆明市住房和城乡建设局网站推广赚佣金的软件排名
  • wordpress旅游类网站模板广州抖音seo
  • 网站建设维护学什么百度seo推广
  • 做视频网站注意什么问题河北网站建设制作
  • 深圳做网站建设的公司深圳互联网公司排行榜
  • 十句经典广告语想找搜索引擎优化
  • 在线图片编辑免费版seo刷排名公司
  • 免费建设网站bt搜索引擎下载
  • 中小企业如何建设网站360站长工具
  • 中国建设银行官方网站汇率最近国际新闻大事20条
  • 申请建设工作网站的函资阳市网站seo
  • 安吉做企业网站深圳知名网络优化公司
  • 网站快速排名方法中国软文网官网
  • 广州做网站信科网络seo的五个步骤
  • 网站建设价格多少企业网络搭建
  • 网站域名设计负面口碑营销案例
  • 网站子域名怎么设置百度指数在哪里看
  • 百度网站两两学一做心得体会营销案例100例小故事及感悟
  • 西藏做网站怎么做好网站方式推广
  • 二手交易平台网站的建设网站推广网络推广
  • 哪一个景区网站做的最成熟外贸seo网站建设
  • 在哪里做网站好软文写手兼职
  • 北京网站建设哪家公司好灰色seo关键词排名
  • 北京网站建设方案品牌公司域名查询注册信息查询
  • 集团公司网站欣赏内部优化
  • 国外幼女和成人做视频网站搜索引擎优化的重要性