Solve the WeChat applet [Component] slot ““ is not found.

solution

When using 自定义组件or slot标签as 组件Athe content of a slot, 组件Aa default slot must be defined in , and there are no restrictions on tags such as ordinary views. And since the slot equivalent wx:iffor falseis not defined

Scenario recurrence (just take the situation I encountered as an example)

1. Debug the basic library 2.19.4
2. Use the component package of weapp, take van-search the components in it as an example
3. When used in any page van-search, the warning will appear in the console [Component] slot "" is not found., which does not affect the running of the program, but just looks uncomfortable

Troubleshooting process

According to the literal meaning of the error, an undefined default slot is used.
Let’s look at the normal default slot use cases.
Examples include: custom componentscustom

//自定义组件  custom
<view>
    <slot></slot>
</view>

homeUsed in another component|pagecustom

//  home
<custom>
    <view>22</view>
</custom>

The above usage is to use the default slot, and the console will not have any warning, even if the customslot in is removed, there will be no warning, but when a custom component or a slot label is used as the content of the slot, and there customis no [Component] slot "" is not found.A warning will appear for the default slot .

Take van-searchas an example, van-searchthere is such a structure in ,
insert image description here
here are two slots as van-filedthe content of the slot, enter and van-filedyou will find that there is no default slot defined, at this time, van-fieldadd a default slot at any position, and the console will not appear after refreshing The previous warning message will appear

Take van-popupanother example, although there is a default slot, but because the initial value of wx:if is false, the initial parsing is skipped
insert image description here

Notice

Adding a slot that does not affect the display is not the best way, just to avoid warning messages

Replenish

Although the setting display:nonecan not display the useless default slot, it may eventually load this piece of useless content, which can be considered for joint use wx:if=“visible”, but visiblethe default value is true, through the timer or other timing after loading, it will visiblebecomefalse
insert image description here

insert image description here
However, this method will introduce another problem
[Component] slot "" duplication is found under a single shadow root. The first one was accepted
Literally, there are two slots, the first one is used, the main code is as follows
insert image description here

It is found that we added to avoid the problem of the code block that the slot is not found abnormally, because when the pop-up window is displayed, the upper and lower slots can be parsed, so give us a hint to tell us, bold 有两个slot,程序不知道要显示那个,因此就给你显示了第一个guess , if the first slot is inappropriate during the code parsing process, but there is a suitable slot later, this prompt will not be given, modify as follows and the
insert image description here
warning will disappear

reference

Analytical descriptions from other bloggers

Summarize

In the final analysis, this is a problem with development tools. Of course, it is understandable to give such warnings. Although it can be solved by hacking, it is not the best way.

Guess you like

Origin blog.csdn.net/weixin_43954962/article/details/122563524