vue教程2 【Vue组件化编程】 组件的创建和使用 非单文件组件和单文件组件的区别

组件

实现局部功能的代码和资源的集合

非单文件组件

1.定义组件(创建组件)

2.注册组件

3.使用组件(写组件标签)

定义

使用Vue.extend(options)创建,其中options和new Vue(options)时传入的有点区别

  1. el不写。最终所有的组件要经过vm的管理,有vm中的el决定哪个容器
  2. data必须写成函数。避免组件复用时,数据存在引用关系
    使用template配置组件结构

注册

局部注册:new Vue时传入components选项

全局注册:使用Vue.component('组件名','组件')

编写组件标签

<school></school>
<body>
<div id="root">
    <!--    使用组件-->
    <employee></employee>
    <hr>
    <department></department>
</div>
</body>
<script>
    //定义组件
    const employee = Vue.extend({
        template: `
          <div>
          <h2>{{ name }}</h2>
          <h2>{{ age }}</h2>
          <button @click="showName">点击提示姓名</button>
          </div>
        `,
        data() {
            return {
                name: '胡梓卓',
                age: 18
            }
        },
        methods: {
            showName() {
                alert(this.name);
            }
        }
    });
    const department = Vue.extend({
        template: `
          <div>
          <h2>{{ name }}</h2>
          <h2>{{ numberOfPeople }}</h2>
          <button @click="showName">点击提示部门名称</button>
          </div>
        `,
        data() {
            return {
                name: '智能制造部',
                numberOfPeople: 203
            }
        },
        methods: {
            showName() {
                alert(this.name);
            }
        }
    });
    new Vue({
        el: '#root',
        //注册组件
        components: {
            employee: employee,
            department: department
        },
        data: {},
        method: {},
        computed: {},
        watch: {},
        filters: {},
        directives: {}
    });
</script>
image-20220112213414805

注意点⚠️

组件名

一个单词组成:

​ (首字母小写):school

​ (首字母大写):School

多个单词组成:

​ (kebab-case):my-school

​ (CamelCase):MySchool

注意:

(1)组件名不要写成html中已有的元素名称

(2)可以使用name配置向指定组件在开发者工具中呈现的名字(第三方组件库)

组件标签
<school></school>
<school/>

不能使用脚手架时,<school/>会导致后续组件不能渲染

声明组件简写
const school = Vue.extend({options}) => const school = {options}

组件嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Document</title>
    <script src="https://cn.vuejs.org/js/vue.js"></script>
</head>
<body>
<div id="root">
    <app></app>
</div>
</body>
<script>
    const student = Vue.extend({
        name: 'student',
        data() {
            return {
                name: '涂鏊飞',
                age: 22
            }
        },
        template: `
          <div>
          <h2>{{ name }}</h2>
          <h2>{{ age }}</h2>
          </div>
        `,
        components: {}
    });
    const hello = Vue.extend({
        name: 'hello',
        template: `
        <h1>介绍</h1>
      `
    });
    const school = Vue.extend({
        name: 'school',
        data() {
            return {
                name: '湖北工程学院',
                adress: '湖北孝感'
            }
        },
        template: `
          <div>
          <div>
            <h2>{{ name }}</h2>
            <h2>{{ adress }}</h2>
          </div>
          <student></student>
          </div>
        `,
        //注册子组件,模板包含student,外部要包含一个根节点
        components: {
            student
        }
    });
    const app = Vue.extend({
        name: 'app',
        template: `
          <div>
          <hello></hello>
          <school></school>
          </div>
        `,
        //管理school和hello
        components: {
            school, hello
        }
    });
    new Vue({
        el: '#root',
        components: {
            app
        },
        data: {},
        method: {},
        computed: {},
        watch: {},
        filters: {},
        directives: {}
    });
</script>
</html>
image-20220113203249371

VueComponent构造函数

  1. school組件本质是一个VueComponent的构造函数

  2. Vue解析时,会帮我们创建school的组件实例对象,即执行new VueComponent(options)

  3. 每次调用Vue.extend,返回的是全新的VueComponent组件实例对象

  4. 关于this指向

(1)组件配置中:
	data,methods,watch,comptued指向【VueComponent组件实例对象】,简称vc
(2)new Vue(options)配置中:
    data,methods,watch,comptued指向【Vue实例对象】
image-20220113210933616

vue实例与组件实例

组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 datacomputedwatchmethods 以及生命周期钩子等。仅有的例外是像 el 这样根实例特有的选项。

内置关系

原型链
function Demo() {
        this.a = 1999;
        this.b = 2000;
    }
    const d = new Demo();
    console.log(d.__proto__);
    console.log(Demo.prototype === d.__proto__);
    Demo.prototype.c = 2022;
    console.log(d.c);
image-20220113212458368
 VueComponent.prototype.__proto__ === Vue.prototype
const app = Vue.extend({});
console.log(app.prototype.__proto__ === Vue.prototype) //true

让组件实例对象可以访问到vue原型上的属性和方法

image-20220113214210545

单文件组件

⚠️main.js导入import App from './App.vue'需要在脚手架环境运行!!!

Uncaught SyntaxError: Cannot use import statement outside a module

School.vue

<template>
  <div>
    <h2>学校名称:{{ name }}</h2>
    <h2>学校地址:{{ address }}</h2>
  </div>
</template>

<script>
export default {
  name: "School",
  data() {
    return {
      name: '湖北工程学院',
      address: '湖北省孝感市'
    }
  }
}
</script>

<style scoped>
h2 {
  color: #0dff1d;
}
</style>

Student.vue

<template>
  <div>
    <h2>学生姓名:{{ name }}</h2>
    <h2>学生年龄:{{ age }}</h2>
  </div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      name: '涂鏊飞',
      age: '22'
    }
  }
}
</script>

<style scoped>
h2 {
  color: #1c036c;
  font-weight: 700;
}
</style>

App.vue

<template>
  <div>
    <school></school>
    <student></student>
  </div>
</template>

<script>
import School from './School'
import Student from './Student'

export default {
  name: "App",
  components: {
    Student, School
  }
}
</script>

main.js

import App from './App.vue'

new Vue({
    el: '#root',
    components: {
        App
    },
    template: `<app></app>`
})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单文件组件</title>
</head>
<body>
<div id="root">
</div>
</body>
<script src="https://cn.vuejs.org/js/vue.js"></script>
<script src="./main.js"></script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

折腾的小飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值