00:00
我们继续啊,还是要说一说setup当中的一些问题的,刚刚咱们是聊一聊setup当中的这个什么呢?它的执行时机是不是,下边咱们来说setup的返回值和它的参数的问题啊,我们先来看这个吧,返回值。呃,首先呢,我们可以看到setup当中返回来的是一个对象。啊,那我找到APP吧,咱在这个APP里边去看啊,这个对象里边放的是属性或者是方法,那这个属性和方法呢,都是给谁呢?给咱们当前这个模板去使用的是吧?哎,那所以说我就在这儿啊,再给大家去总结,接着往后总结。嗯,在这说setup。中的返回值啊,是一个对象。内部的属性和方法是给给什么呢?给这个HTM模板使用的,哎,好,这是一个问题。下一个问题啊,在这儿呢,咱们说这个什么呢?返回对象中的属性会与date函数中返回对象的属性合并成为组件对象的属性啊,这个是什么意思啊,咱们来看。
01:05
如果说我在这儿啊,再加上一个逗号,再来一个date,咱们V2当中用的不就是date吗。呃,那我在这儿给他返回来一个count吧,来,我来一个十。之后呢,先在这儿来看一下,来一个H3,那我就写个count括号,括号括号我就写上一个count啊,然后一保存,我们先来看看这个界面吧。嗯,刷新大家可以看一下这个count是个十啊,那也就是说我这个date当中return对象里边的count数据和我setup当中return对象里边的这个某个属性是吧,或者说和这个属性他们都可以在我的模板当中去使用啊,这是没有问题的,所以说在这啊。Setup中的对象内部的属性和date函数中的return I对象。
02:03
的这个属性都可以在什么呢?HTM模板中使用啊,而且呢,呃,像我们这个文档里边所说的啊,他们呢,会合并,合并成为组件对象的属性啊,再加上一个。所以说setup中的对象中的数据啊,就是属性和date函数中的对象中的这个属性会合并。合并,合并。为什么呢?组件对象的属性啊好,那这一点怎么看的呢?咱们只能这么看啊,比如说我来一个什么呢?Mounted。嗯,这不也是生命周期回调吗?是吧?他是哪一个了?还记得吗?这个是界面渲染后的生命周期回调啊,诶,我要CAno.log括号,我们输出一个Z来,大家看一下啊。
03:04
没保存啊。然后我们去刷新一下。好在这呢啊。Proxy看到了吗?咱们现在这个VIEW3里边,如果说我要是使用的话,那这个this其实也变成了一个代理对象啊,我点开它,点开这个target,我们可以看一下这块有个count,还有个message。MSG是吧,也是有的啊好,嗯,他俩会合并啊,都变成这个什么呢?组件对象的属性啊,再往后看,比如说我们在这啊再来一个。我来一个什么呢?来一个这个。Met hods啊。它这个里边是专门存放方法的是吧,诶那我们就写上一个吧呢,比如说叫收MSG。括号pencil.log个括号,这个是methods中的收MSG方法。之后呢,我在这个setup当中吧,我也写上一个方法,那比如说来一个count的收MSG。
04:07
这个我写成个二。等于。can.log括号,这个是setup中的收MSG1方法,那我也就写上这么两句话啊,咱们来看一下。然后还是要刷新。点开找到这个他给他我们来看看啊,这有个兽MG2看到了吧,哎,在这儿呢,还有没有兽MG1啊。好像没看着。一个两个。二是谁呀,二是methods当中的啊,对,因为咱们这个setup里边没有去写的是吧,我把这个收MSG1拿过来,收MSG1放在这。保存一下,我们再来看一下,这样就应该出现了。找到它,然后找到这个,诶两个是不是就都有了吧?哎,然后我们把这条也总结一下啊。
05:05
这是什么呢?是setup中的对象中的方法和MY对象对象中的这个方法啊。呃,会合并为组件对象的这个方法。好,这又是一条了是吧,还是蛮多的啊,那反这个如果有重名三大优先,这个大家可以去测啊,可以去测一下好,一般不要混合用。啊,这块咱们还是加上吧,把这个加上啊,就什么呢?哎,在这个V3中尽量不要混合的使用什么呢?Date和set up及method和set up,因为这个date里边的属性在setup当中可以用是吧,你可以去写,就是说date里边的属性我在setup当中去定义也是没有问题的,那我们ma当中的方法在setup当中去定义也是没有问题的,但是咱们尽量不要混着用。
06:06
因为已经到V3了嘛,所以说咱们还是推荐在setup当中去写属性和方法的,嗯,还有一个。Setup不是一个,不能是一个函数。然后呢,因为反回值不再是return的对象了啊,这个是啥意思呢?是这个意思,咱们这它是一个函数嘛。那它是一个函数的话,如果加上a think之后,那反过来应该是一个promise。所以说这个要注意啊,不能这么干,哎,那咱们把这条直接拿过来吧。直接放到这儿啊。啊放这啊这个要注意。嗯,那我们再来看啊这个,但这块啊一一般不要混合用,这个刚才咱们已经说过了,但在setup方法中不能访问data和method里边的东西。为什么呢?咱们想啊。为啥不能访问呢?如果说我要是在setup当中去访问data里边的count,那是不是得通过类似方式去访问呢?而我们能访得到吗?访问不到啊,访问不到,为什么?因为咱们之前说过,说set up在执行的时候是在before之前,这个时候组件还没有被创建出来,因为组件没有创,被创建出来组件的实际对象根本就用不了this。
07:16
是不是this如果用不了的话,那this还能找到date里边的count吗?还能找到这个message当中的受M2嘛,是找不到的啊,所以说最后的话,这一条咱们就加上吧。我也就放到这儿啊。这个我总结的可能比较细一些啊,所以说大家在看的时候呢,还是可以尽量做一个筛选的一个操作是吧,你自己筛选一下,然后把这个内容呢,再去整理一下笔记啊,再去记一下啊行,那关于这个setup返回值的这块呢,咱们先说到这儿,回头之后呢,咱们再聊一聊setup当中参数的问题。
我来说两句