使用vant的时候,报错:component has been registered but not used以及vant的使用方法总结

在使用vant的时候。 想按需引入,于是安装了babel-plugin-import插件。

文档:https://youzan.github.io/vant/#/zh-CN/quickstart

但是遇到了上述报错。 不在components中注册,或者用这种常用的方式注册,都会报错:

//示例
import { Button } from 'vant'//引入
components:{ Button } //注册

注意:文档都没有写引入的组件的注册部分,没有完整的使用实例。

具体报错如下:

解决方案1(只有少量使用vant组件的时候,可以考虑这个,因为一个个引入有些麻烦)

手动引入,需要单独引入组件和css。 组件的路径文件夹名称有时候还需要自己找。

路径是:node_modules_vant@2.6.0@vant\lib

还要单独的引入对应的css样式。

特别需要注意的是注册方式需要是:

[组件名.name]:组件名

<template>
<div>
this is for test words!
<van-swipe-cell>
<template #left>
<van-button square type="primary" text="选择" />
</template>
<van-cell :border="true" title="单元格" value="内容" />
<template #right>
<van-button square type="danger" text="删除" />
<van-button square type="primary" text="收藏" />
</template>
</van-swipe-cell> <van-button type="default">默认按钮</van-button>
<van-button type="primary">主要按钮</van-button>
<van-button type="info">信息按钮</van-button> </div>
</template>
<script>
import Button from 'vant/lib/button';
import SwipeCell from 'vant/lib/swipe-cell';
import Cell from 'vant/lib/cell'; import 'vant/lib/swipe-cell/style';
import 'vant/lib/cell/style';
import 'vant/lib/button/style';
export default {
name: "Login",
components: {
[Button.name]:Button,
[SwipeCell.name]:SwipeCell,
[Cell.name]:Cell
}
};
</script>

需要引入暴露的组件名。但是测试发现可以是任意的,

因为swipe-cell中没有搜索到找到SwipeCell。

但是在button中有export“Button”

例如:

import XButton from 'vant/lib/button';//引入
[XButton.name]:XButton//注册

所以只需要保持一致就行。

该解决方案参考了:

在官方在github中提供的demo中,发现局部注册有像这样写的。

https://github.com/youzan/vant-demo/blob/master/vant/base/src/view/cart/index.vue

解决方案2 (这会让打包后的文件体积大一点,但是确实是最方便的方式。如果不考虑模块化,工程化开发而不介意把它挂载到vue原型的话)

main.js

import Vue from 'vue'
import App from './App.vue'
import Vant from 'vant';
import 'vant/lib/index.css'; Vue.use(Vant);
Vue.config.productionTip = false new Vue({
render: h => h(App),
}).$mount('#app')

demo.vue

<template>
<div>
this is for test words!
<van-swipe-cell>
<template #left>
<van-button square type="primary" text="选择" />
</template>
<van-cell :border="true" title="单元格" value="内容" />
<template #right>
<van-button square type="danger" text="删除" />
<van-button square type="primary" text="收藏" />
</template>
</van-swipe-cell> <van-button type="default">默认按钮</van-button>
<van-button type="primary">主要按钮</van-button>
<van-button type="info">信息按钮</van-button> </div>
</template>
<script> export default {
name: "Login",
components: { }
};
</script>

不用导入,不用注册,直接用.

解决方案3(推荐)也是开篇使用babel-plugin-import插件遇到的问题的解决方法

其实这不算解决方案。只是以前用vue的时候,组件注册这里有个小细节一直没有注意导致的。

快速查看示例代码:

<template>
<div>
this is for test words!
<van-swipe-cell>
<template #left>
<van-button square type="primary" text="选择" />
</template>
<van-cell :border="true" title="单元格" value="内容" />
<template #right>
<van-button square type="danger" text="删除" />
<van-button square type="primary" text="收藏" />
</template>
</van-swipe-cell> <van-button type="default">默认按钮</van-button>
<van-button type="primary">主要按钮</van-button>
<van-button type="info">信息按钮</van-button> </div>
</template>
<script>
import { Button,SwipeCell,Cell } from 'vant'
export default {
name: "Login",
components: {
'van-button':Button,//对应<van-button>标签
'van-swipe-cell':SwipeCell,//对应<van-swipe-cell>标签
'van-cell':Cell//对应<van-cell>标签 //也可以像这样写
// vanButton:Button,
// vanSwipeCell:SwipeCell,
// vanCell:Cell }
};
</script>

之所以,出现“component has been registered but not used”这个报错,就是因为使用了components:{ Button }进行注册。

这种注册方式注册的是Button这个元素,但是在dom上确实没有这个元素。 因为是van-button这个元素。 而这个元素又没有被正确注册 。

也就是说<van-button>标签、<van-swipe-cell>标签、<van-cell>标签都没有被正确的注册导致的。

你可以像这样来注册组件:

'van-button':Button,//对应<van-button>标签
'van-swipe-cell':SwipeCell,//对应<van-swipe-cell>标签
'van-cell':Cell//对应<van-cell>标签

还可以用驼峰命令法,首字母大小写都可以,省略" - " 来进行注册。如:

vanButton:Button,
vanSwipeCell:SwipeCell,
vanCell:Cell
或者
VanButton:Button,
VanSwipeCell:SwipeCell,
VanCell:Cell

该解决方案参考:

https://cn.vuejs.org/v2/guide/components-registration.html (#组件名大小写)

总结:

在vue中,带短横线的标签元素的注册。必须是驼峰命令法,或者用引号括起来。

如<van-button>的注册必须是
vanButton:Button
或者VanButton:Button
再或者'van-button':Button

使用vant的时候,报错:component has been registered but not used以及vant的使用方法总结的更多相关文章

  1. VUE编译报错 Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead

    背景: 在使用VUE添加标签的时候编译报错,报错如下: Component template should contain exactly one root element. If you are u ...

  2. Vue脚手架报错 Component name "Student" should always be multi-word vue/multi-word-component-names

    报错信息分析: 新手在第一个次使用脚手架的时候难免会遇到各种各样奇怪的问题,最近在学习Vue的过程中就出现了如下问题 通过阅读报错信息可知: 是我们的组件名有一些问题,(报错信息翻译过来大概就是组件名 ...

  3. ubuntu 修改保存报错E37:No write since last change(add ! to override)的解决方法

    报错信息如下: E37: No write since last change (add ! to override) 解决办法是: 在修改完后,将命令 :q 改成 :wq 即可.

  4. (原创)vagrant up 异常报错,出现 There was an error while executing `VBoxManage` 的解决方法

    最近在使用 vagrant homestead 时,不小心在虚拟机上使用了 exit 命令退出虚拟机,导致再使用 vagrant up 时出现以下错误: Bringing machine 'larav ...

  5. Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...

  6. Archlinux/Manjaro使用笔记-使用makepkg安装软件 报错:未找到strip分割所需的二进制文件 的解决方法

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 使用archlinux或manjaro安装aurman时遇到如下报错 错误:未找到strip分割所需的二进制文件 原因:未安装g ...

  7. Win7 64bit 安装VisualSVN出现报错:Servic &#39;VisualSVN Server&#39; failed to start.解决的方法

    问题描写叙述: Win7 64bit 安装VisualSVN时出现报错: Servic 'VisualSVN Server' failed to start.Please check VisualSV ...

  8. 报错org.openqa.selenium.WebDriverException: disconnected: unable to connect to renderer解决方法

    做自动化时经常会遇到不兼容的问题,比如以下简单的脚本,主要是打开浏览器,然后最大化窗口,打开百度,输入内容搜索,代码如下: package com.gs.selenium; import org.op ...

  9. mac下python环境pip报错[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) 的解决方法

    1.mac下python环境pip报错: issuserdeMacBook-Pro:~ issuser$ pip install pyinstallerCollecting pyinstaller  ...

随机推荐

  1. 写于疫情期间的一个plantUML例子

    @startuml 这几天的正经事 start repeat if(思维清晰) then (yes) :刷题; else (no) if(想写程序) then (yes) :调项目; else (no ...

  2. 2019计蒜客信息学提高组赛前膜你赛 #2(TooYoung,TooSimple,Sometimes Naive

    计蒜客\(2019CSP\)比赛第二场 巧妙爆零这场比赛(我连背包都不会了\(QWQ\) \(T1\) \(Too\) \(Young\) 大学选课真的是一件很苦恼的事呢! \(Marco\):&qu ...

  3. C# RSACryptoServiceProvider 加密解密 RSA 加密解密

    什么是RSA:RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥 推导出 解密密钥在计算上是不可行的”密码体制. 下附代码,在控制台中粘贴在启动类即 ...

  4. 【Weiss】【第03章】练习3.3:通过交换指针交换单/双链表元素

    [练习3.3] 通过之调整指针(而不是数据)来交换两个相邻的元素,使用 a.单链表 b.双链表 Answer: 先放测试代码,折叠标题可以看到分别是哪种链表的测试. 实测可满足题意,但单链表和双链表的 ...

  5. ASP.NET MVC5实现芒果分销后台管理系统(二):Code First快速集成EntityFramework

    在上一篇文章中,我们已经搭建了整个芒果后台管理系统整个工程架构,并集成了AutoMapper,日志组件等,接下来我们将使用Entity Framework完善系统的持久化存储部分.这篇EF的构造,我将 ...

  6. 懂一点Python系列——快速入门

    本文面相有 一定编程基础 的朋友学习,所以略过了 环境安装.IDE 搭建 等一系列简单繁琐的事情. 一.Python 简介 Python 英文原意为 "蟒蛇",直到 1989 年荷 ...

  7. Natas19 Writeup(Session登录,常见编码,暴力破解)

    Natas19: 提示,与上一题源码类似,只是PHPSESSID不连续.随便输入username和password,抓包观察PHPSESSID,发现是输入的信息,按照id-username的格式,由a ...

  8. python关于字典如何格式化地写入文件之中

    1.python关于字典如何式化地写入文件之中 如何写入:https://blog.csdn.net/qq_15642411/article/details/79943741 (推荐使用json包) ...

  9. 如何简单的将手机投屏在windows上(可在电脑上直接操作手机)

    首先附上要使用的scrcpy源地址 接下来是如何使用(我用的是安卓手机+win10): 下载好后,首先使用数据线连接手机到电脑,并且手机需要打开开发人员选项(不知道如何打开的自行百度): 打开到安装s ...

  10. Js遍历数组总结

    Js遍历数组总结 遍历数组的主要方法为for.forEach.map.for in.for of for var arr = [1,2,3,4,5]; var n = arr.length; // 直 ...