vue2和vue3在html中引用组件component方式不一样

我的vue版本是:20.17.0

一、在HTML中,引用组件格式区别

vue2引用组件可以是file.vue格式,需要导入:<script src="https://unpkg.com/http-vue-loader"></script>才可以识别vue格式。

vue3引用组件格式是:file.js。

二:vue2引用列子

1、html代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>vue2引用组件例子</title>
		<script type="text/javascript" src="./vue2框架包/vue.js" ></script>
		<script type="text/javascript" src="./vue2框架包/http_vue_loader.js" ></script>
	</head>
	<body>

		<div id="app">
			<h1>Hello Vue</h1>
			<h1>{{msg}}</h1>
			<testvue/>

		</div>

		<script>

			var v=new Vue({

				el:"#app",
				components:{
					"testvue": httpVueLoader("./testvue.vue")
				},
				data:function(){
					return{
						msg:"hello"
					}
				}


			})


		</script>

	</body>
</html>

2、vue 代码:

<template>
	<div>欢迎您!!{{msg}}</div>
</template>

<script>
	module. Exports= {

				data: function(){

					return{
						msg:"child component"
					}

				}
			}
</script>

3、页面渲染结果:     

                   ​​​​​​​         

三、vue3引用列子

1、html代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>vue3 组件导入</title>
		<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

	</head>
	<body>

		<div id="app">
			<h1>Hell Vue3</h1>
			<h1>{{msg}}</h1>
			<test/>

		</div>

		<script type="module">
			import test from "./testvue.js"

			 const {createApp,ref,watch} = Vue
			 createApp({
			 	components:{
			 		test  //也可以:"test":test
			 	},
			 	data:function(){
			 		return{
			 			msg:"hello world"
			 		}
			 	}

			 }).mount('#app')

		</script>
	</body>
</html>

2、js代码:

const dd=`
<div>
<h1>wo shi child component</h1>
<h2>{{msg}}</h2>
</div>`

export default {
	data: function () {
		return {
			msg: 'child component'
		}

	},
  	template: dd
}

3、页面渲染结果:

        ​​​​​​​        ​​​​​​​        

是的,Vue 2 Vue 3 在 v-model 对组件的用法上有一些同。 在 Vue 2 中,使用 v-model 绑定组件时,组件内部需要定义名为 `value` 的 prop,并且在组件内部通过 `$emit` 方法触发名为 `input` 的自定义事件来更新父组件的数据。例如: ```vue // 父组件 <template> <custom-component v-model="data"></custom-component> </template> <script> export default { data() { return { data: &#39;&#39; }; } }; </script> // 子组件 CustomComponent.vue <template> <input :value="value" @input="$emit(&#39;input&#39;, $event.target.value)" /> </template> <script> export default { props: [&#39;value&#39;] }; </script> ``` 而在 Vue 3 中,v-model 对组件的使用更加灵活,并且再需要定义名为 `value` 的 prop。可以通过使用 `v-bind` `v-on` 指令来实现自定义的双向绑定。例如: ```vue // 父组件 <template> <custom-component v-model="data"></custom-component> </template> <script> export default { data() { return { data: &#39;&#39; }; } }; </script> // 子组件 CustomComponent.vue <template> <input :value="modelValue" @input="$emit(&#39;update:modelValue&#39;, $event.target.value)" /> </template> <script> export default { props: [&#39;modelValue&#39;] }; </script> ``` 可以看到,在 Vue 3 中,父组件通过 `v-model` 绑定时,子组件接收的 prop 名称为 `modelValue`,并且通过在 `@input` 事件中使用 `update:modelValue` 自定义事件来更新父组件的数据。 总之,Vue 3 中的 v-model 对组件的用法更加灵活,并且再需要在组件内部定义名为 `value` 的 prop。这使得在使用自定义组件时更加简洁易用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值