首页 > 其他分享 >vue中<script setup>中使用watch、computed、props、defineExpose、defineEmits等方法

vue中<script setup>中使用watch、computed、props、defineExpose、defineEmits等方法

时间:2023-05-24 10:24:07浏览次数:38  
标签:vue const computed script props defineEmits import ref

<script setup> 是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖。相比于普通的 <script> 语法,它具有更多优势:

  • 更少的样板内容,更简洁的代码。
  • 能够使用纯 TypeScript 声明 props 和抛出事件。
  • 更好的运行时性能 (其模板会被编译成与其同一作用域的渲染函数,没有任何的中间代理)。
  • 更好的 IDE 类型推断性能 (减少语言服务器从代码中抽离类型的工作)。

一些基本用法可以前往Vue3官网查看

props

  • 基本用法:
<script setup>
  const props = defineProps({
    foo: String
  })
</script>
//  通过props传过来的是一个静态值,如果需要赋值给双向绑定的数据
<script setup>
  import { toRefs } from 'vue';
    
  const props = defineProps({
    foo: String
  })
  beaselineId.value = toRefs(props).foo.value;
</script>
  • 带默认值的props
export interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two']
})

defineExpose()

可以通过 defineExpose 编译器宏来显式指定在 <script setup> 组件中要暴露出去的属性:

  • Vue2:
this.$refs.create.openModal()
  • Vue3:
// 组件内部
<script lang="ts" setup>
  import { ref } from 'vue';

  const visible = ref<boolean>(false);
  const openModal = () => {
    visible.value = !visible.value;
  };

  defineExpose({ openModal });
</script>

// 外部调用
<create-global ref="createModal" />

<script lang="ts" setup>
  import { ref } from 'vue';
  import CreateGlobal from './create/create-global.vue';

  const createModal = ref();

  const openModal = () => {
    createModal.value.openModal();
  };
</script>

defineEmits()

父组件

//getGatewayData要获取的参数
<tree :show="show" 
  @gatewayData="getGatewayData">
</tree>
//执行方法获取参数
const getGatewayData = (e) => {
  console.log('getGatewayData', e)
}

子组件

import { ref, defineEmits } from 'vue'
const emits = defineEmits(['handleNodeClick'])
const handleNodeClick = (e) => {
 emits('gatewayData', '子组件的数据')
}

标签:vue,const,computed,script,props,defineEmits,import,ref
From: https://www.cnblogs.com/ZerlinM/p/17427241.html

相关文章

  • vue this.$set的作用
    在Vue.js中,this.$set是一个用于在Vue实例中设置响应式属性的方法。它允许您在不重新创建整个对象的情况下添加新的响应式属性。Vue.js通过观察对象的属性来追踪其变化,从而实现数据的响应式。然而,当您添加一个新的属性时,Vue无法自动追踪该属性的变化。这就是this.$set方法派上用场......
  • 基于springboot+vue数码论坛系统设计与实现、论坛管理系统,附源码+数据库+lw文档+PPT
    1、项目介绍考虑到实际生活中在数码论坛方面的需要以及对该系统认真的分析,将系统权限按管理员和用户这两类涉及用户划分。(1)系统功能需求登录系统后,主要模块包括首页、数码板块、数码评价、数码论坛、畅聊板块、新闻资讯、个人中心、后台管理等功能。系统功能用例图如图3-1所示......
  • #yyds干货盘点#JavaScript的数学对象——Math对象
    Math对象●js给我们提供了一些操作数字的方法●也是一种数据类型是复杂数据类型●Math对象的通用语法:Math.xxx()random()●Math.random()这个方法是用来生成一个0~1之间的随机数●每次执行生成的数字都不一样,但是一定是0~1之间的●生成的数字包含0,但是不包含1var......
  • 解析 Pinia 和 Vuex
     Pinia和Vuex一样都是是vue的全局状态管理器。其实Pinia就是Vuex5,只不过为了尊重原作者的贡献就沿用了这个看起来很甜的名字Pinia。本文将通过Vue3的形式对两者的不同实现方式进行对比,让你在以后工作中无论使用到Pinia还是Vuex的时候都能够游刃有余。既然我们要对比两者的实现......
  • Compile NSIS scripts in Linux
    http://blog.alejandrocelaya.com/2014/02/01/compile-nsis-scripts-in-linux/ CompileNSISscriptsinLinuxNSISisawellknownsystemusedtocreateWindowsinstallersforanytypeofapplicationbasedonscripts.Itisgoodsystem.Oncewehavedefinedour......
  • 我的第一个项目(十三) :组件间传值的一些方案(vuex,eventbus,localStorage)
    好家伙, 先说一下我的需求,我要组件间传值 1.eventBus前端兄弟组件传值eventbus无法使用不报错也不触发,就很奇怪//eventBus.jsimportVuefrom"vue";exportdefaultnewVue();//Mylogin.vue<buttontype="button"class="btnbtn-primary"@click="login&quo......
  • vue3.0组件封装
    组件全局祖册1.建立公共文件夹my-ui2.index.js文件导出全局祖册组件的install方法3.main.js中impotindex.js导入install方法使用并useimportmyUifrom'./components/my-ui'createApp(App).use(myUi).mount('#app')......
  • ele+vue简单操作
    绑定值和字符串拼接:1、:title="`字符串${xx}`"2、:title="'字符串'+xx"3.异步回调setTimeReflush:function(){var_this=this;returnnewPromise(function(resolve,reject){$.post(BaseUrl+'&#......
  • vue中使用scss公共变量的方法 :export
    vue中使用scss公共变量的方法:export:https://blog.csdn.net/weixin_44698285/article/details/124051066?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-124051066-blog-125074100.235%5Ev36%5Epc_relevant_def......
  • Cesium结合GIS天地图 加载倾斜摄影3dtile + vue3
    实现思路将倾斜摄影OSGB数据转换为3dtile(转换方式很多,可以利用第三方工具cesiumlab)利用Cesium加载GIS地图,我这里使用的是天地图,可以加载其他地图都行加载3dtile数据到地图中展示安装插件npminstallcesium加载地图以加载天地图为例,需要先到天地图官网去申请开发者,获取......