前端入门之VUE--vue组件化编程

前言

  • VUE是前端用的最多的框架;
  • 这篇文章是本人大一上学习前端的笔记;
  • 欢迎点赞 + 收藏 + 关注,本人将会持续更新。

文章目录

2、Vue组件化编程

2.1、组件

组件:用来实现局部 功能的代码资源的集合

2.2、基本使用
  • 三大步骤:
    1. 定义组件(常见组件)
    2. 注册组件
    3. 使用组件(写组件标签)
html 复制代码
<div id="root">
    //3.写组件标签
    <school></school>
    <student></student>
</div>
<script>
    //1.创建school组件
    const school = Vue.extend({
        //不要写el
        template: `
			<div class="demo">
			<h2>学校名称:{{schoolName}}</h2>
			<h2>学校地址:{{address}}</h2>	
			</div>
		`,
        data(){
            return {
                schoolName: '尚硅谷',
                address: '北京昌平'
            }
        }
    }),
    //1.创建student组件
    const student = Vue.extend({
        template:`
				<div>
					<h2>学生姓名:{{studentName}}</h2>
					<h2>学生年龄:{{age}}</h2>
				</div>
		`,
        data() {
            return {
                studentName: 'cy',
                age: 20
            }
}
    })
    
    //创建vm
    new Vue({
        el: '#root',
        
        //2.注册组件(局部注册)
        components: {
            school,
            student
        }
    })
</script>

关于组件名:

  • 一个单词组成
    • 第一种写法(首字母小写)
    • 第二种写法(首字母大写)
  • 多个单词组成:
    • 第一种写法:如:my-school
    • 第二种写法:如:MySchool(需要Vue脚手架支持)

注意:组件名尽可能避免HTML已经有的元素名称

  • 关于组件标签:
    • 第一种写法:
    • 第二种写法:
html 复制代码
<body>
		<div id="root">
		</div>
</body>

	<script type="text/javascript">
		Vue.config.productionTip = false
		
		//定义student组件
		const student = Vue.extend({
			template:`
				<div>
					<h2>学生名称:{{name}}</h2>	
					<h2>学生年龄:{{age}}</h2>	
				</div>
			`,
			data(){
				return {
					name:'JOJO',
					age:20
				}
			}
		})

		//定义school组件
		const school = Vue.extend({
			template:`
				<div>
					<h2>学校名称:{{name}}</h2>	
					<h2>学校地址:{{address}}</h2>	
					<student></student>
				</div>
			`,
			components:{
				student
			},
			data(){
				return {
					name:'尚硅谷',
					address:'北京'
				}
			}
		})

		//定义hello组件
		const hello = Vue.extend({
			template:`
				<h1>{{msg}}</h1>
			`,
			data(){
				return {
					msg:"欢迎学习尚硅谷Vue教程!"
				}
			}
		})

		//定义app组件
		const app = Vue.extend({
			template:`
				<div>
					<hello></hello>
					<school></school>
				</div>
			`,
			components:{
				school,
				hello
			}
		})

		//创建vm
		new Vue({
			template:`
				<app></app>
			`,
			el:'#root',
			components:{
				app
			}
		})
	</script>
</html>
2.2.1、VueComponent
  1. school组件本质是一个名为VueComponent的构造函数,且不是程序员定义的,是Vue.extend生成的
  2. 特别注意:每次调用Vue.extend,返回的都是一个全新的VueComponent!
  3. 关于this指向:
    1. 组件配置中:data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是VueComponent实例对象
    2. new Vue(options)配置中:data函数、methods中的函数、watch中的函数、computed`中的函数 它们的this均是Vue实例对象

VueComponent.prototype.proto === Vue.prototype

相关推荐
cxxcode21 小时前
从 V8 引擎视角理解微任务与宏任务
前端
destinying1 天前
性能优化之实战指南:让你的 Vue 应⽤跑得飞起
前端·javascript·vue.js
徐小夕1 天前
JitWord Office预览引擎:如何用Vue3+Node.js打造丝滑的PDF/Excel/PPT嵌入方案
前端·vue.js·github
晴殇i1 天前
揭秘JavaScript中那些“不冒泡”的DOM事件
前端·javascript·面试
孟陬1 天前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
BER_c1 天前
前端权限校验最佳实践:一个健壮的柯里化工具函数
前端·javascript
兆子龙1 天前
别再用 useState / data 管 Tabs 的 activeKey 了:和 URL 绑定才香
前端·架构
sudo_jin1 天前
前端包管理器演进史:为什么 npm 之后,Yarn 和 pnpm 成了新宠?
前端·npm
敲敲敲敲暴你脑袋1 天前
写个添加注释的vscode插件
javascript·typescript·visual studio code
叁两1 天前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent