Vue学习日记part6 Vue3新特特性-Setup、生命周期钩子、provide

Setup

语法:

Setup(props,context){}

setup 函数会在 beforeCreate 之后、created 之前执行

注意:在 setup() 函数中无法访问到 this

参数:

1:props:是响应式的,当传入新的 prop 时,它将被更新(因为 props 是响应式的,你不能使用 ES6 解构,因为它会消除 prop 的响应性)

export default {
  props: {
    title: String
  },
  setup(props) {
    console.log(props.title)
  }
}

需要销毁 prop (可以通过使用 setup 函数中的 toRefs 来安全地完成此操作)

import { toRefs } from 'vue'

setup(props) {
	const { title } = toRefs(props)
	console.log(title.value)
}

2:context(是一个普通的 JavaScript 对象,它暴露三个组件 property,它不是响应式,可以对它进行解构)

export default {
  setup(props, context) {
    // Attribute (响应式 object)
    console.log(context.attrs)
    // Slots (响应式 object)
    console.log(context.slots)
    // Emit 事件 (方法)
    console.log(context.emit)
  }
}

解构写法

export default {
  setup(props, { attrs, slots, emit }) {
    ...
  }
}

 attrs 和 slots 是有状态的对象,在组件本身更新时总是更新它们。这意味着你应该避免对它们进行解构,并始终将属性引用为 attrs.x 或 slots.x

如果你打算根据 attrs 或 slots 更改应用副作用,那么应该在 onUpdated 生命周期钩子中执行此操作。

创建vue3.0

  • npm i -g @vue/cli
  • vue create mydemo
  • 接下来按回车,安装

列子1

reactive()函数接收一个普通对象,返回一个响应式的数据对象。

<template>
  <div class="hello">
    <div>{{ readersNumber }} {{ book.title }}</div>
  </div>
</template>

<script>
import { ref, reactive } from "vue";
export default {
  name: "HelloWorld",
  setup() {
    const readersNumber = ref(1);
    const book = reactive({ title: "Vue 3" });

    // expose to template
    return {
      readersNumber,
      book,
    };
  },
};
</script>

注意,从 setup 返回的 refs 在模板中访问时是 Ref 展开,因此不应在模板中使用 .value

列子2

<template>
  <div class="hello">
  </div>
</template>

<script>
import { h, ref, reactive } from "vue";
export default {
  name: "HelloWorld",
  setup() {
    const readersNumber = ref(0);
    const book = reactive({ title: "Vue 3" });
    // Please note that we need to explicitly expose ref value here
    return () => h("div", [readersNumber.value, book.title]);
  },
};
</script>

在 setup() 内部,this 不会引用活动的实例,因为在解析其他组件选项之前调用了 setup(),因此 this 内部 setup() 的行为与其他选项中的 this 完全不同。

 

生命周期钩子生命周期2x生命周期3x

选项 APIHook inside inside setup
beforeCreateNot needed*
createdNot needed*
beforeMountonBeforeMount
mountedonMounted
beforeUpdateonBeforeUpdate
updatedonUpdated
beforeUnmountonBeforeUnmount
unmountedonUnmounted
errorCapturedonErrorCaptured
renderTrackedonRenderTracked
renderTriggeredonRenderTriggered

TIP:setup 是围绕 beforeCreate 和 created 生命周期钩子运行的,所以不需要显式地定义它们。

<template>
  <div class="hello"></div>
</template>

<script>
import { onMounted, onBeforeMount } from "vue";
export default {
  name: "HelloWorld",
  setup() {
    onBeforeMount(() => {
      console.log("Component is onBeforeMount!");
    });
    // mounted
    onMounted(() => {
      console.log("Component is mounted!");
    });
  },
};
</script>

 

provideinject使用 Provide提供/注入 (Provide / Inject)

//HellowWorld.vue

<template>
  <div class="hello">
    <MyMarker />
  </div>
</template>

<script>
import { provide, reactive, ref, readonly } from "vue";
import MyMarker from "./MyMarker.vue";

export default {
  components: {
    MyMarker,
  },
  setup() {
    const location = ref("North Pole");
    const geolocation = reactive({
      longitude: 90,
      latitude: 135,
    });

    provide("location", readonly(location));
    provide("geolocation", readonly(geolocation));
  },
};
</script>

//MyMarker.vue

<template>
  <div>
    {{userLocation}}---{{userGeolocation.longitude}}---{{userGeolocation.latitude}}
  </div>
</template>
<script>
import { inject } from "vue";

export default {
  setup() {
    const userLocation = inject("location");
    const userGeolocation = inject("geolocation");

    return {
      userLocation,
      userGeolocation
    };
  },
};
</script>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值