全部的错误是 Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
(found in )
原因分析:1、没有注册组件,即did you register the component correctly ?
2、书写组件的方式不对 。(如果注册么问题,就是书写组件方式的问题)

  • 如何注册组件?

使用Vue的component函数注册一个组件,定义代码如下:第一个参数为组件名称“name”,第二个为组件详细的配置信息
名称两种定义方式
1、my-component 全小写(就按全小写名称引用组件)
2、驼峰命名,如MyComponent (注意:首字母也要大写)

Vue.component("myComponent",{
			data:function(){
				return {
					count:0
				}
			},
			template:"<button v-on:click='count++'>你点了{{count}}</button>"
		})
		
  • 如何引用?

在DOM中直接使用错误案例:<MyComponent></MyComponent>
这里着重说明错误原用,我定义时是MyComponent,引用不能这么引用,vue会帮你把大写C转成“-c” 。所以如果驼峰定义组件的话,书写组件的时候用“-”链接驼峰之间的名称,并以小写的形式书写,即应该<my-component></my-component>这么引用。注意这里的两种情况:
- 这是直接在DOM中使用,引用时必须小写
- 如果是在字符串模版中,可以<MyComponent></MyComponent>这么引用,即两种方式均可。

  • 报错的原因就是,你引用的<myComponent></myComponent>vue找不到你定义它的名称,即就提醒你 make sure to provide the “name” option.。
  • 官方文档如下:

    当使用 PascalCase (首字母大写命名) 定义一个组件时,你在引用这个自定义元素时两种命名法都可以使用。也就是说 和 都是可接受的。注意,尽管如此,直接在 DOM (即非字符串的模板) 中使用时只有 kebab-case 是有效的。

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐