请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

为什么new Vue放在最上面会报错呢

<script type="text/javascript">
	var app = new Vue({
		el:"#app"
	})
	Vue.component('hello',{
		data:function(){
			return{
				text:'hello'
			}
		},
		template:'<div>{{text}}</div>'
	})
	Vue.component('world',{
		data:function(){
			return{
				text:''
			}
		},
		template:'<div>world</div>'
	})
</script>
像这样写,就会报错![图片描述](http://img1.sycdn.imooc.com//szimg/5bfd6071000180ff04660161.jpg)
但是如果把var app = new Vue这个写在最后面就不会报错,像这样:
Vue.component('hello',{
		data:function(){
			return{
				text:'hello'
			}
		},
		template:'<div>{{text}}</div>'
	})
	Vue.component('world',{
		data:function(){
			return{
				text:''
			}
		},
		template:'<div>world</div>'
	})
	var app = new Vue({
		el:"#app"
	})
	此时就不会报错,并且数据可以显示在页面上,这是为什么呢

正在回答

2回答

Dell 2018-11-30 00:18:47

回复 qq_Amelia_1:你把报错的代码截图完整贴上来

0 回复 有任何疑惑可以回复我~
  • 提问者 慕神1662884 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2018-12-03 19:28:29
  • 提问者 慕神1662884 #2
    [Vue warn]: Unknown custom element: <hello> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
    
    (found in <Root>)
    [Vue warn]: Unknown custom element: <world> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
    
    (found in <Root>)
    回复 有任何疑惑可以回复我~ 2018-12-03 19:29:38
Dell 2018-11-28 00:18:52

因为这个时候你Vue这个变量还不存在,vue库还没引入

0 回复 有任何疑惑可以回复我~
  • 提问者 慕神1662884 #1
    可是我在这个地方已经引入过了呀
    <head>
    	<meta charset="utf-8">
    	<script src="https://unpkg.com/vue@2.5.13/dist/vue.js"></script>
    </head>
    回复 有任何疑惑可以回复我~ 2018-11-29 21:31:10
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信